Extending the user profile in Django. Admin creation of users

前端 未结 1 860
盖世英雄少女心
盖世英雄少女心 2021-01-03 04:54

Good evening,

I am presently creating a site with Django and I extended the user with a user profile. I have a small problem though. Here is my situation:

相关标签:
1条回答
  • 2021-01-03 05:21

    By default, empty inline is permitted and thus no further check would be taken for an empty form. You need to override it manually:

    class UserProfileForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super(UserProfileForm, self).__init__(*args, **kwargs)
            if self.instance.pk is None:
                self.empty_permitted = False # Here
    
        class Meta:
            model = UserProfile
    
    
    class UserProfileInline(admin.TabularInline):         
        model = UserProfile                               
        form = UserProfileForm  
    
    0 讨论(0)
提交回复
热议问题