I have a UserAdmin, and I defined a UserProfileInline like this:
from ...
from django.contrib.auth.admin import UserAdmin as UserAdmin_
class UserProfileInLine(admin.StackedInline):
model = UserProfile
max_num = 1
can_delete = False
verbose_name = 'Profile'
verbose_name_plural = 'Profile'
class UserAdmin(UserAdmin_):
inlines = [UserProfileInLine]
My UserProfile model has some required fields.
What I want is to force the user not only to enter the username & repeat password, but also to enter at least the required fields so that the UserProfile instance is created and associated to the User that is being added.
If I enter anything in any field of UserProfileInline when creating the user, it validates the form without problem, but if I don’t touch any field, it just creates the User and nothing happens with the UserProfile.
Any thoughts?
Check recent answer Extending the user profile in Django. Admin creation of users , you need to set the
empty_permittedattribute of theformof the inline , to beFalse. Just likeAnother possible solution could be to create your own
Formset(that inherits fromBaseInlineFormSet), like suggested in this link.It could be something like that:
Then specify that formset in the
InlineModelAdmin:The good thing about this second option is that if the UserProfile model doesn’t require any field to be filled, it will still ask for you to enter any data in at least one field. The first mode doesn’t.