1

Say I have two different models like...

class Model1(models.Model):
    field1 = models.CharField(max_length=32)
    field2 = models.CharField(max_length=16)

class Model2(models.Model):
    field3 = models.CharField(max_length=32)
    field4 = models.CharField(max_length=16)

However, upon a sign up form for example I want them to input information that corresponds to field1, field2 and field3 of course which spreads over two separate models.

forms.py

class Form1 (forms.ModelForm):
    ...
    class Meta:
        model = Model1, Model2 #note that this does not work...:(
        fields = ['field1', 'field2', 'field3']

How can I get this to work so that I can receive data from two different models on the same one form?

tripleee
  • 175,061
  • 34
  • 275
  • 318
jayt
  • 758
  • 1
  • 14
  • 32
  • See [this question](http://stackoverflow.com/questions/2770810/multiple-models-in-a-single-django-modelform). Basically just make two `ModelForm` classes and render them both on the page. A `ModelForm` is meant for only one model – Jessie Apr 11 '17 at 20:29
  • You can send multiple forms from your view to your template. – Matt Cremeens Apr 11 '17 at 20:31
  • Create a fake model with all required fields from both models and implement save method to fill and save your "real" models – iurii_n Apr 11 '17 at 20:45

0 Answers0