# Saving multiple models from one form in django

**URL:** <https://forum.djangoproject.com/t/saving-multiple-models-from-one-form-in-django/2615>\
**Category:** Forms & APIs\
**Created:** [May 25, 2020, 9:11am UTC](https://forum.djangoproject.com/t/saving-multiple-models-from-one-form-in-django/2615 "2020-05-25T09:11:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![waboke](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/waboke/32/4433_2.png) [@waboke](https://forum.djangoproject.com/u/waboke)\
**Post date:** [May 25, 2020, 9:11am UTC](https://forum.djangoproject.com/t/saving-multiple-models-from-one-form-in-django/2615/1 "2020-05-25T09:11:02Z")

</div>

I am new to django and am trying to develop a web app for a farm management. My form i want to register a farm. i have three models, which are related and i want them to be save to the database from on function.

The model has three models as Phonenumbers, address and farm.  
Model.py

```auto
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime, date
class Farms(models.Model):
    farm_name = models.CharField(max_length=200, blank = True)
    recorded_by = models.ForeignKey(User, default= None, null=True, on_delete=models.CASCADE)

    def __str__ (self):
        return self.farm_name

COUNTRY_CHOICES = (
    ('Nig','Nigeria'),
    ('AF', 'Afghanistan'),
    ('AL' ,'Albania'),
    ('DZ' , 'Algeria'),
    ('AX' , 'Aland'),
    ('AS' , 'American'),
    ('AI' , 'Anguilla'),
    ( 'AD' , 'Andorra'),
    ('AO' , 'Angola'),
    ('AN' ,' Antilles - Netherlands'),
)
class Address(models.Model):

    street_name = models.CharField(max_length = 200, blank = True)
    street_no = models.CharField(max_length = 50, blank = True)
    city = models.CharField(max_length = 200)
    state = models.CharField(max_length = 200)
    country = models.CharField(max_length = 200, choices=COUNTRY_CHOICES, default='Nig')
    farms = models.ForeignKey(Farms, on_delete=models.CASCADE)

class Telephone(models.Model):
    phonenumber = models.CharField(max_length = 200)
    farms= models.ForeignKey(Farms, on_delete=models.CASCADE)

    def __str__ (self):
        return self.phonenumber

Form.py
from django import forms
from .import models

class CreateFarmsForm(forms.ModelForm):
    class Meta:
        model = models.Farms
        fields = ['farm_name']

class TelephoneCreateForm(forms.ModelForm):
    class Meta:
        model = models.Telephone
        fields = ['phonenumber']
        labels = {
            'phonenumber': 'Phone Number'
        }

```

```auto
class AddressCreateForm(forms.ModelForm):
    class Meta:
        model = models.Address
        fields = ['street_name', 'street_no', 'city', 'state','country']
        labels = {
            'street_name': 'Street Name',
            'street_no': 'street No'
        }

class DateInput(forms.DateInput):
    input_type = "date"

    def __init__ (self, **kwargs):
        kwargs["format"] = "%Y-%m-%d"
        super(). __init__ (**kwargs)

```

```auto
Farm.html
{% extends "livestock/livestock_base.html" %}
{% load crispy_forms_tags %}
 {% block livestock%}
                      <div class="card-body">
                             <div class="row">
                                 <div class="col-md-6 offset-3 ">
                          <form class="" action="" method="post">
                               {% csrf_token %}
                                  <div class="row">
                              <div class="form-group col-md-8 ">
                              {{farm_form.farm_name | as_crispy_field}}
                                  </div>
                              <div class="form-group col-md-4">
                                   {{phone_form.phonenumber | as_crispy_field}}
                                  </div>
                                      </div>
                              <div class="row">
                              <div class="form-group col-md-8 ">
                              {{address_form.street_name | as_crispy_field}}
                                  </div>
                                  <div class="form-group col-md-4 ">
                              {{address_form.street_no | as_crispy_field}}
                                  </div>
                                  </div>
                                <div class="row">
                              <div class="form-group col-md-6">
                              {{address_form.city | as_crispy_field}}
                                  </div>
                                    <div class="form-group col-md-6">
                              {{address_form.state | as_crispy_field}}
                                  </div>
                                  </div>
                              <div class="form-group ">
                              {{address_form.country | as_crispy_field}}
                                  </div>
                              <div class="form-group">
                                <button type="submit" class="btn btn-primary btn-block btn-sm" >Submit</button>
                                </div>
                          </form>
                                 </div>
                             </div>
                      </div>
{% endblock livestock%}

```

Views.py

```auto
def farm_name(request):
    if request.method == 'POST':
        farm_form = forms.CreateFarmsForm(request.POST)
        phone_form = forms.TelephoneCreateForm(request.POST)
        address_form = forms.AddressCreateForm(request.POST)
        if farm_form.is_valid and phone_form.is_valid and address_form.is_valid:
            farm=farm_form.save(commit=False)
            farm.recorded_by = request.user
       
            phone=phone_form.save(False)
            phone.farm = farm
            phone.save()
            address = address_form.save(False)
            address.farm = farm
            address.save()
            
    farm_form = forms.CreateFarmsForm()
    phone_form = forms.TelephoneCreateForm()
    address_form = forms.AddressCreateForm()
    context = {'farm_form':farm_form, 'phone_form':phone_form, 'address_form':address_form,}
    return render(request, "livestock/farm_name.html", context)

```

I have been on it for the past two days. please i need some help to continue.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 25, 2020, 9:57am UTC](https://forum.djangoproject.com/t/saving-multiple-models-from-one-form-in-django/2615/2 "2020-05-25T09:57:49Z")

</div>

What is the issue or error that has you stalled at this point? Are you getting some error message indicating a problem? Or is something happening that you’re not expecting to see happen?

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [May 25, 2020, 10:13am UTC](https://forum.djangoproject.com/t/saving-multiple-models-from-one-form-in-django/2615/3 "2020-05-25T10:13:17Z")

</div>

> [@waboke](#):
>
> `if farm_form.is_valid`

You mean `is_valid()` - the parentheses are necessary to call the function, otherwise this staement is always true.

---

<div class="post-metadata">

**Author:** ![waboke](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/waboke/32/4433_2.png) [@waboke](https://forum.djangoproject.com/u/waboke)\
**Post date:** [May 25, 2020, 11:08am UTC](https://forum.djangoproject.com/t/saving-multiple-models-from-one-form-in-django/2615/4 "2020-05-25T11:08:59Z")

</div>

The issue was resolved. i replaced false with commit=false

---

<div class="post-metadata">

**Author:** ![ramiboutas](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ramiboutas/32/4295_2.png) [@ramiboutas](https://forum.djangoproject.com/u/ramiboutas)\
**Post date:** [April 21, 2021, 8:38pm UTC](https://forum.djangoproject.com/t/saving-multiple-models-from-one-form-in-django/2615/5 "2021-04-21T20:38:18Z")

</div>

I would suggest the following;

- Rename the model Farms to Farm
- Call the app name to farms, for me it makes more sense
- put some comment on what you are doing (especially in the views, this helps begginers like me to understand what’s going on)

I rewrite your code for me to understand the problem and send push it to gitHub in case somebody is interested in knowing how this works 🙂

> **[ramiboutas/django\_forum\_farm](https://github.com/ramiboutas/django_forum_farm)**
>
> Contribute to ramiboutas/django\_forum\_farm development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![NinJagtap](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ninjagtap/32/8199_2.png) [@NinJagtap](https://forum.djangoproject.com/u/NinJagtap)\
**Post date:** [April 20, 2022, 1:40am UTC](https://forum.djangoproject.com/t/saving-multiple-models-from-one-form-in-django/2615/6 "2022-04-20T01:40:50Z")

</div>

Greetings,

Further extending the above question - for example, if I have one more model “Produce” with a 1-many relationship between “Address” and “Produce”.

How can I implement a Produce model form - where the user can select Farm and Address from the dropdown to enter and save Produce detail - on one form?

(Note: There is no direct relationship between Farm and Produce model - Farm can access the Produce model by inner join with Address.)

Regards,
