Skip to content

Instantly share code, notes, and snippets.

View collinanderson's full-sized avatar

Collin Anderson collinanderson

  • South Bend, Indiana
View GitHub Profile
#!/usr/bin/env python
from django.db.models.aggregates import Aggregate
from django.db.models.sql.aggregates import Aggregate as SqlAggregate
from django.db.models.sql.query import Query
class SqlGetter(SqlAggregate):
sql_function = ''
class SqlQuery(Query):
collinanderson / serialize_object
Created October 21, 2011 18:10
def serialize_object(obj):
args = []
for field in obj._meta.fields:
value = field.value_from_object(obj)
if value and value != field.default:
from decimal import Decimal
if isinstance(value, unicode):
value = str(value)
except UnicodeEncodeError:

Fix locale problem

Run the following command

$ update-locale LC_ALL="en_US.UTF-8"

If it failed, you will need to add the following to /var/lib/locales/supported.d/local file

en_US.UTF-8 UTF-8

collinanderson /
Created August 12, 2014 12:43 +
#!/usr/bin/env python
import os
import site
import sys
sys.dont_write_bytecode = True # don't write .pyc files
sys.argv[0] = os.path.abspath(__file__) # show full path in ps and top
os.environ['DJANGO_SETTINGS_MODULE'] = 'demo.settings'
1:20 PM <collinanderson> i had one really, really crazy idea that i almost posted on the thread back in the day. why no just have a _meta.fields that _is_ an OrderedDict of _all_ (non related object?) fields. it would be name -> field_instance
1:21 PM <collinanderson> that way you don't even need .get_field() (singular). you just say _meta.fields['field_name']
1:21 PM <collinanderson> though you would likely do a lot of iterating over _meta.fields.values()
1:22 PM <collinanderson> that way it would preserve the order
server {
root /path/to/php;
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php5-fpm.sock;
collinanderson /
Last active August 29, 2015 14:11
admin search
qs = Model.objects.all()
for kw in q.split():
q = models.Q()
for f in fields:
q |= Q('%s__iexact' % kw)
qs = qs.filter(q)
collinanderson / python3
Last active August 29, 2015 14:11
Alternative Packages with Python 3 support.
Most of these packages are forks of the original.
MySQL-python -> mysqlclient
suds -> suds-jurko
django-registration -> django-registration-redux
fabric -> no support yet?
gevent -> no support yet?
akismet -> pykismet3
django-storages -> django-storages-redux
xlwt -> xlwt-future
collinanderson /
Last active August 29, 2015 14:13
username length hack
AbstractUser._meta.get_field('username').max_length = 75 # HACK
for v in AbstractUser._meta.get_field('username').validators:
if getattr(v, 'limit_value', '') == 30:
v.limit_value = 75
class User(AbstractUser):
class Meta:
db_table = 'auth_user'
Collins-MBP:tests collin$ PYTHONPATH=.. python3 ./ --failfast migrations.test_commands
Testing against Django installed in '/Users/collin/django1.8/django'
Creating test database for alias 'default'...
Creating test database for alias 'other'...
FAIL: test_makemigrations_with_custom_name (migrations.test_commands.MakeMigrationsTests)
Traceback (most recent call last):
File "/Users/collin/django1.8/tests/migrations/", line 691, in test_makemigrations_with_custom_name