Skip to content

Instantly share code, notes, and snippets.

from django.db import router
from django.db.models import deletion
def ALLOW_MIGRATE_CASCADE(collector, field, sub_objs, using):
opts = sub_objs[0]._meta
if router.allow_migrate(using, opts.app_label, opts.model_name):
deletion.CASCADE(collector, field, sub_objs, using)
@charettes
charettes / test.py
Created December 23, 2015 23:12
Python 3.5 OrderedDict bug
import collections
import gc
import weakref
def create_reference():
class A(object):
pass
class B(A):
parents = collections.OrderedDict([
@charettes
charettes / gist:6296424
Created August 21, 2013 16:03
Registering mutant dynamic models to reversion.
from django.dispatch.dispatcher import receiver
from mutant.signals import mutable_class_prepared
import reversion
@receiver(mutable_class_prepared)
def register_reversion(sender, existing_model_class, **kwargs):
reversion.register(sender) # Register the newly created model class
if obsolete_class: # Unregister the obsolete model class
reversion.unregister(existing_model_class)
cls = self.__class__
if klass is None:
klass = cls
elif not not issubclass(klass, cls):
klass = type(klass.__name__, (klass, cls), {})
class MyManagerBase(Manager.__class__):
pass
class MyManager(Manager):
__metaclass__ = MyManagerBase
@charettes
charettes / qs.py
Created February 5, 2013 21:35
Django #15363
def deprecated_get_query_set(cls):
def get_query_set(self, *args, **kwargs):
self.trace.append("%s.get_query_set" % cls.__name__)
return cls.get_queryset(self, *args, **kwargs)
cls.get_query_set = get_query_set
# Account for already defined __getattribute__
getattribute = getattr(cls, '__getattribute__')
def __getattribute__(self, name):
attribute = getattribute(self, name)
if name == 'get_queryset':
@charettes
charettes / generate.py
Created December 20, 2012 20:31
Django ticket #19482 localflavor apps locales
#!/usr/bin/env python
from __future__ import unicode_literals
import os
import re
import shutil
from subprocess import Popen
from django.contrib import localflavor
import polib
from polib import POEntry
@charettes
charettes / django-github-pull-ticket-linkify.js
Last active October 3, 2015 21:28
Linkify ticket numbers in django github pull request
// ==UserScript==
// @name Django github ticket number linkify
// @namespace django-github-ticket-linkify
// @description Linkify ticket numbers to django's track on github
// @grant none
// @include https://github.com/*/django/*
// ==/UserScript==
const TICKET_NUMBER_LINKIFY = ' <a target="_new" href="https://code.djangoproject.com/ticket/$1">#$1</a>';
@charettes
charettes / authuser.md
Created April 3, 2012 17:46 — forked from jacobian/authuser.md
Upgrading auth.User - the profile approach

Upgrading auth.User - the profile approach

This proposal presents a "middle ground" approach to improving and refactoring auth.User, based around a new concept of "profiles". These profiles provide the main customization hook for the user model, but the user model itself stays concrete and cannot be replaced.

I call it a middle ground because it doesn't go as far as refactoring the whole auth app -- a laudable goal, but one that I believe will ultimately take far too long -- but goes a bit further than just fixing the most egregious errors (username length, for example).

This proposal includes a fair number of design decisions -- you're reading the fifth or sixth draft. To keep things clear, the options have been pruned out and on the one I think is the "winner" is still there. But see the FAQ at the end for some discussion and justification of various choices.

The User model

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>Django Docs Search</ShortName>
<Description>Search in Django Documents</Description>
<InputEncoding>UTF-8</InputEncoding>
<Image width="16" height="16">data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAapJREFUOI2lk09I02EYxz/P+/78uSxQUnfISsibHjqI4C7u0KZCkwoFMVL0kOAlPIlg4CEI8aztqB4UCqQYbY4IPBRLLwV1NAJJFPOghenU358Oc6MfmxPqe3rg/T6f9/nDI+Mzk+7rD2/4F0UCrUjjYMgtZlKiAHBcp/B7sWTTKGE1mmTl2RJaaVJTcVaml/CZvpzHKAYQERBBsmbDyFV0LqDiUjla6UyyZBDBR3cRIH2cLgxQSjHQ3kNXsIPqiso8aGJiAVFC+0g36eOjfMCDUBdDd/qxbItX7xLs7v+kr60brTJlX7xQhlJFWogEWgGYSz4nGpsF4H6oE63M06FknbnAu4Wq8ssAfNtazx/KGfIAtnd3AAg0NOUZRcTzc1aeFl4sxxjrHSYSCHOzrp69/V+YRgkAPrMUdboN27ELA16+j2M7Nj237nHjSi3X/DUcHqX5/mOT6/6rAPw+PODEOikMAIilksRSSUQErTSWbaFEePrwMQCfvn45u4W/5boulm2hlebJwCihxhYc12H+7aJ3NucdE2Ru4nZzmPXtDT6uffYC/vec/wDjbXl0qXpOzgAAAABJRU5ErkJggg==</Image>
<Url type="text/html"