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
- [Answered ]-Django: Form values to redirect to url in single step
- [Answered ]-Understanding prefetch_related with my current example
- [Answered ]-How to generate a responsive PDF with Django?
- [Answered ]-Failed to interpret date regular expression
- [Answered ]-Why can't I not use the now variable in bug fix for django poll tutorial part 5
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.
- [Answered ]-Django: Insert new row with 'order' value of the next highest value avoiding race condition
- [Answered ]-Images in quiz model