Monday, June 29, 2009

Adding a field to the django auth user.

I've been trying to do this for a while and I keep finding how-to's such as Extending the Django User model with inheritance, but the problem with these methods is that you still cant sign-in to the django admin with any users you create (such as the admin you set up at the beginning of the project).

I finally got the monkey-patching method to work by doing the following.

You can add fields to the user class with the method add_to_class instead of inheriting the User class by doing the following in your models.py:
User.add_to_class('new_field', models.BooleanField(default=False))

Once that is done, in admin.py, modify the UserAdmin and add your new field
from django.contrib.auth.admin import UserAdmin

UserAdmin.list_display += ('new_field',)
UserAdmin.list_filter += ('new_field',)
UserAdmin.fieldsets[1][1]['fields'] = ('first_name','last_name','email','new_field')
VoilĂ , with that done, you're new fields should show up in the auth user on the django admin page.

No comments:

Post a Comment