[Answered ]-Extending auth.User model, proxied fields, and Django admin

2👍

Keep it simple. Here’s an example of a UserProfile model from a library we use:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    accountcode = models.PositiveIntegerField(null=True, blank=True)

That’s it. Don’t bother with the __getattr__ override. Customise the admin interface instead:

from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class UserProfileInline(admin.StackedInline):
    model = UserProfile

class StaffAdmin(UserAdmin):
    inlines = [UserProfileInline]
    # provide further customisations here

admin.site.register(User, StaffAdmin)

This allows you to CRUD the User object, with access to the UserProfile as an Inline. Now you don’t have to proxy attribute lookups from the UserProfile to the User model. To access the UserProfile from an instance of User u, use u.get_profile()

0👍

This is not a proxy class, it is a relationship. See more on Proxy Models, which are a subclass of the original model, with the Meta.proxy = True

0👍

If you just want a field from User to be in list_display in your UserProfileAdmin, try:

class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('user__email',)

If you want to have it as part of the form, add it into your UserProfileForm as an extra field, and validate it in the form.

Leave a comment