Kyle->GetThoughts();



Django patch to use emails, not usernames
8 January 2009 @ 10:47 PM MST
Current Music: None
Current Mood: Alright
Oh, this patch was created for Django 1.0, it may need to be tweaked for older/newer versions.

I've been working with Django recently. For my project I would prefer to use only email addresses and not separate usernames. There are a couple of patches to try and attach this functionality, but most of them seem to just hack in a way to use either for logging in, but don't make provisions for uniqueness or adjustments to the admin interface.

I decided to just create a patch that alters the default installation to use only email addresses, usernames are removed.

There are two patches. One for the django.contrib.admin package and one for the django.contrib.auth package.
django_admin_email.patch
django_auth_email.patch

** UPDATE 1/10/09 **
Thanks to BJ's suggestion I looked into using __getattr__. With some finagling and guess and check I have been able to make User.username an alias of User.email. So you can read User.username and set User.username and you only really see or change User.email. That should make sure that compatibility with other modules still holds.
** END UPDATE 1/10/09 **

Just copy them into the django/contrib directory (you'll need root access) and then run:
# sudo patch -p0 < django_admin_email.patch
# sudo patch -p0 < django_auth_email.patch

(Or you can just run them from whatever location so long as after the '<' you have '/path/to/patch')

On Ubuntu 8.10 the django/contrib directory you want is located at:
/usr/share/python-support/python-django/django/contrib

I've never created a patch before, but I used The Ten Minute Guide to diff and patch . So if you need more help on how to use these patches I'd recommend trying there, I used their guide to create them.

The changes affect the admin interface and allow you to follow the Django Tutorial fully (at least as it stands as of this writing).

If you'd like any more information on this feel free to contact me either by leaving a comment here or by emailing me. My email address is kyle (dot) dickerson _at_ gmail =dot= com.

[This Entry]


Replies: 1 Comment

 On Saturday, January 10th @ 08:39, BJ Homer said:
I'd suspect that if you defined the getattr method (see http://docs.python.org/reference/datamodel.html#customizing-attribute-access) on the user class, you could make user.username return user.email pretty easily. I haven't looked into it too much, but that seems like an easy enough way of fixing the problem.