In one of your other models, create the mixin code. This adds these fields to your application's version of the User.
from django.contrib.auth.models import User
User.add_to_class("company", models.ForeignKey(Company, blank=True, null=True))
User.add_to_class("title", models.CharField(max_length=100, blank=True))
User.add_to_class("company_admin", models.BooleanField(default=False))
Of course, you'll need to add the fields to the database manually.
Now, to add them to the admin. The following code takes the Newforms Admin's version of the UserAdmin and adds some extra stuff.
from django.contrib.auth.admin import UserAdmin
UserAdmin.list_display += ('company','title','company_admin')
UserAdmin.list_filter += ('company_admin')
UserAdmin.fieldsets += (
('Company Info', {'fields': ('company', 'title', 'company_admin')}),
)
By using Mixin's without a lot of code, you can add to User model and still use the exact same shortcuts and decorators (get_profile() still works.)
Inspiration came from http://www.amitu.com/blog/2007/july/django-extending-user-model/ and some extra work to get it to work with Newforms Admin.
1 comments:
Nice work Tim!
The only thing that tripped me up was your comment "In one of your other models, create the mixin code."
I tried adding the mixin code to my models file, but got a Django error about my field not being present in the form.
I ended up moving the mixin code to my admin.py and everything worked fine.
Thank you!
Post a Comment