Django: "Cannot add foreign key constraint"

Upgraded to Django 1.8 and now you get this error when you run your tests? Have you created migrations for all your apps? If not, you may well be hitting the problem that the database tables are being created in the wrong order, which will give you this error. If you have an existing Django 1.7 or earlier project, then you need to create the initial migration files, and then fake the initial migration, as described here

Migrating from django-registration to django-allauth

I’ve been wanting to migrate away from django-registration for a while. Yes, I know it’s been the default registration package since forever and I’ve used it happily for many years. But : * it's no longer actively maintained * I want to do away with the email confirmation * I want to allow login using either email or username So after a bit of googling, I decided to try out django-allauth, and I’ve just finished the migration.

Replace user-input field with enforced value in Django form

I’m subclassing someone else’s form; I need to remove their input field and replace it with a fixed value. class MyForm(TheirForm): def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) # remove unwanted field self.fields.pop('fixedfield') def clean(self, *args, **kwargs): cleaned_data = super(MyForm, self).clean(*args, **kwargs) # Restore field with fixed value cleaned_data['fixedfield'] = FIXEDVALUE return cleaned_data Simples…


I like BDD. I like cucumber. I like python. I like django. I don’t like lettuce, a commonly-used python port of cucumber, when used with django. They’ve chosen the completely daft option to use your main database when testing. I’d forgotten this, so when I discovered I’d torched my production database just because I wanted to write a test, I decided to not touch lettuce again… And then I discovered behave, which works fine as a python BDD tool; except it doesn’t have an easy tie-up with django.

Django GeoIP templatetag

I’ve published 2 template tags for use with GeoIP at Hope they may be of use… Usage: Templatetag get_country_name returns the client’s country code # Example: {% ifequal get_country “GB” %} do something {% endifequal %} Templatetag get_country sets the given variable name to the client’s country code # Example: {% get_country as my_country %} {% ifequal my_country “GB” %} do something {% endifequal %}

Django/Ajax: a great simple tutorial

An excellent tutorial from James Bennett for your first step into combining AJAX with Django “A step-by-step walk through a simple AJAX form with Django backend”

Django search engine

To help me get to grips with Django quickly, I created a search engine of the documentation. On the off-chance this is useful to others, I made it available at (Sorry if this does not show up as a hyperlink, WordPress appears to be chewing that up and ignoring it)