D
D
Deleting Account2019-10-05 12:48:25
Django
Deleting Account, 2019-10-05 12:48:25

Django 2. How can I make the registration form inherit from 2 models, for example the built-in user model and my UserProfile model?

There are 2 form classes:

class ExtendedUserCreationForm(UserCreationForm):
  email = forms.EmailField(required = True)

  class Meta:
    model = User
    fields = ['username','email','password1','password2']

  def save(self,commit = True):
    user = super().save(commit = True)

    user.email = self.cleaned_data['email']
    
    if commit:
      user.save()
    return user 

class UserProfileForm(forms.ModelForm):
  class Meta:
    model = UserProfile
    fields = ('first_name','last_name','father_name')

And I need to make sure that instead of two classes there is one. And at the same time, all the fields of 2 models must be placed in one variable fields. I need this in order to sort the form fields in the template in the sequence I need. At the same time, I want to save the first_name and last_name fields both in the user model table and in the UserProfile table.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2019-10-05
@Andriy_Kosmenyuk

Create a Form (not a ModelForm, just a Form) with the set of fields you need. Implement the save method in it, where the specified fields are decomposed into the necessary models.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question