title: How to post one html form data into two models in django?
body: <p>I having a html form consisting of some fields with details and I want to post some details of the form to one model and some details to another model how this can be done?
  my models.py</p>
  
  <pre><code>class room(models.Model):
      id = models.IntegerField(primary_key=True)
      image = models.ImageField(upload_to='images')
      content = models.CharField(max_length=50,default='0000000')
      created_at = models.DateTimeField(default=datetime.now)
      updated_at = models.DateTimeField(default=datetime.now)
  
  
  
      def __str__(self):
          return self.content
  
  # This is the model for goals   
  class goal(models.Model):
      id=models.IntegerField(primary_key=True)
      goal = models.CharField(max_length=50,default='0000000')
      created_at = models.DateTimeField(default=datetime.now)
      updated_at = models.DateTimeField(default=datetime.now)
  
      def __str__(self):
          return self.goal
  
  # This is the model for designs
  class design(models.Model):
      id=models.IntegerField(primary_key=True)
      image = models.ImageField(upload_to='images')
      content = models.CharField(max_length=50,default='0000000')
      created_at = models.DateTimeField(default=datetime.now)
      updated_at = models.DateTimeField(default=datetime.now)
  
  
      def __str__(self):
          return self.content
  
  # This is the model for furniture
  class furniture(models.Model):
      id=models.IntegerField(primary_key=True)
      phrase=models.CharField(max_length=60,default='111111')
      created_at = models.DateTimeField(default=datetime.now)
      updated_at = models.DateTimeField(default=datetime.now)
  
  
      def __str__(self):
          return self.phrase
  
  # This is the users model
  class user(models.Model):
      username=models.CharField(max_length=20)
      email=models.CharField(max_length=50,unique=True)
      password=models.CharField(max_length=50,default='0000000')
      created_at = models.DateTimeField(default=datetime.now)
      updated_at = models.DateTimeField(default=datetime.now)
  
      def __str__(self):
          return self.username
  
  class UserRequirement(models.Model):
      id=models.IntegerField(primary_key=True)
      user=models.ForeignKey(user,on_delete=models.CASCADE)
      rooms = models.ForeignKey(room,on_delete=models.CASCADE)
      goals = models.ManyToManyField(goal)
      styles = models.ManyToManyField(design)
      furn = models.ForeignKey(furniture,on_delete=models.CASCADE)
      created_at = models.DateTimeField(default=datetime.now)
      updated_at = models.DateTimeField(default=datetime.now)
  </code></pre>
  
  <p>My views.py for posting:</p>
  
  <pre><code>def user_register(request):
      if request.method == 'POST':
          user_form = UserForm(data=request.POST)
          if user_form.is_valid():
              username=request.POST["username"]
              email = request.POST['email']
              password = request.POST['password']
              rooms = request.POST['room']
              g=goals=request.POST['goal']
              g = g.split(',')
              s=styles=request.POST['style']
              s=s.split(',')
              furn=request.POST['furn']
              u = user(username=username,password=password,email=email)
              u.rooms=room.objects.get(pk=rooms)
              goals = goal.objects.filter(pk__in=g)
              styles = design.objects.filter(pk__in=s)
              u.furn = furniture.objects.get(pk=furn)
              u.save()
              u.goals.add(*goals)
              u.styles.add(*styles)
              messages.success(request,'Your project design has been registered')
              return render(request,'register.html')
          else:
              messages.warning(request,'Cant be registered this email already exists')
              return render(request,'register.html')
  </code></pre>
  
  <p>My form.html is</p>
  
  <pre><code>&lt;form action="{% url 'modsy:user_register' %}" method="POST"&gt;
              {% csrf_token %}
              &lt;div class="form-group"&gt;
                &lt;label for="username"&gt;Username&lt;/label&gt;
                &lt;input type="text" name="username" class="form-control" required&gt;
                &lt;div id="uname_error"&gt;&lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                &lt;input type="hidden" name="room" id="name" value=""&gt;
              &lt;/div&gt;
              &lt;div class="form-group" &gt;
                &lt;input type="hidden" name="goal" id="goal" value=""&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                &lt;input type="hidden" name="style" id="style" value=" "&gt;
              &lt;/div&gt;
              &lt;div class="form-group" &gt;
                &lt;input type="hidden" name="furn" id="furn" value=""&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                &lt;label for="email"&gt;Email&lt;/label&gt;
                &lt;input type="text" name="email" class="form-control" required&gt;&lt;br&gt;
                &lt;div id="name_error" style="color:red;"&gt;&lt;/div&gt;&lt;/div&gt;
              &lt;div class="form-group"&gt;
                &lt;label for="password2"&gt;Password&lt;/label&gt;
                &lt;input type="password" name="password" class="form-control" required&gt;
                &lt;div id="pwd_error" style="color:red;"&gt;&lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="button"&gt;&lt;input type="submit" value="Save the Project" style="background-color:#000080;" class="btn btn-secondary btn-block" onclick="return validation(form)"&gt;
            &lt;/form&gt;
  </code></pre>
  
  <p>Now here I want to post the username email and password to the user model and the user room goal furniture style should be stored in the user_requirement model how it can be done?</p>
  
  <p>My forms.py</p>
  
  <pre><code>from django import forms
  from . models import user
  from django.contrib.auth.models import User
  from . models import UserRequirement
  from . models import room
  from . models import goal
  from . models import design
  from . models import furniture
  
  
  
  
  class UserForm(forms.ModelForm):
      class Meta:
          model = user
          fields = ('email',)
  
      def clean_email(self):
          # Get the email
          email = self.cleaned_data.get('email')
  
          # Check to see if any users already exist with this email as a username.
          try:
              match = User.objects.get(email=email)
          except User.DoesNotExist:
              # Unable to find a user, this is fine
              return email
              raise forms.ValidationError('This email address is already in use.')
  
  class UserRequirementForm(forms.ModelForm):
      class Meta:
          model = UserRequirement
          fields=(user,rooms,goals,styles,furn)
  </code></pre>