Django: is it possible to check and save fields of several forms in one general view?

I have 2 models:

class Person( models.Model ):
  username = models.CharField
  name = models.CharField( max_length = 30 )
  surname = models.CharFields( max_length = 30 )
  ...

class PersonSkills( models.Model ):
  person = models.ForeignKey( Person )
  skill = models.CharField( max_length = 30 )
  ...

I would like to add data to Person and PersonSkills in one view.

Now I have a RegisterView like the following:

class RegisterForm( ModelForm ):
    class Meta:
        model = Person
        fields = ( 'username', 'name', 'surname', 'password', )

class RegisterView( FormView ):
    form_class = RegisterForm
    success_url = "/welcome/"
    template_name = "register.tmpl"
    is_valid = True
    def form_valid( self, form ):       
        form.save()
        self.is_valid = True
        return super( RegisterView, self ).form_valid( form )
    def form_invalid( self, form ):
        self.is_valid = False       
        return super( RegisterView, self).form_invalid( form )
    ...

So how can I add data to Person and PersonSkills in one view? Inheritance from FormView allows only one form_class class.

+3
source share
1 answer

The best way to achieve what you want is to create a custom form that declares the fields needed for both models. Use the saveform method to save in separate classes of models, referring to the fields in cleaned_data.

FormView , . , . , *clean*.

+1

All Articles