I've extended the standard Django User model with a UserProfile, and created a OneToOne relationship from the UserProfile to the User, like so:
class UserProfile(models.Model):
user = models.OneToOneField(adminmodels.User)
about = models.TextField('About the author', blank=True)
picture = models.ImageField('Picture (70px x 70px)', blank=True, upload_to='uploads/profile_pics', default='noone.png')
user_site = models.URLField(blank=True, verify_exists=False)
clas开发者_如何学运维s Meta:
verbose_name = "Profiel"
verbose_name_plural = "Profielen"
To get the corresponding admin form included in the User admin, I created an inline admin form, and added it to the user admin, like so:
# Inline admin for the user profile
class UserProfileInline(admin.StackedInline):
model = UserProfile
fk_name = 'user'
max_num = 1
extra = 0
# Include the extra form
class MyUserAdmin(admin.ModelAdmin):
inlines = [UserProfileInline, ]
# Re-register the user form
admin.site.unregister(admin.models.User)
admin.site.register(admin.models.User, MyUserAdmin)
Than, lastly, to save an instance of UserProfile, I have a function connected to postsave. It looks like this:
def on_user_was_saved(sender,instance,created,using,*args,**kwargs):
if type(instance) == adminmodels.User:
if created:
profile = UserProfile.objects.get_or_create(user = instance)
profile.save()
else:
profile = UserProfile.objects.get_or_create(user = instance)
profile[0].save()
Now, two things go wrong:
- When saving the user with anything in the user profile field, the user is saved, and so is the instance of UserProfile, but the instance has been saved as if all fields were empty. When entering something new in these fields you can save that though, nothing goes wrong than.
- A second instance of the UserProfile form becomes available; max_num is ignored.
After benjaoming's input I've changed my post_save callback to:
try:
profile = UserProfile.objects.get(user = instance)
profile.save()
except:
profile = UserProfile.objects.get_or_create(user = instance)
profile[0].save()
However this doesn't solve either of the problems I already had.
The main issue here is that you're not actually processing the profile form. Look at the code you've written: at no point do you have anything to take the values that you've entered for the user profile and save them to the database.
Also, admin.StackedInline
is not built for the purpose you're trying to use it for. That manages one-to-many relationships, not one-to-one relationships.
If you must have the functionality in the admin follow exactly the setup that you are outlining, probably your best shot is to add a custom view to AdminSite
with the forms for adding and editing users. Alternatively, have people edit two forms to create a user.
Anyway, you can read more about adding views to an AdminSite
.
精彩评论