# Automatic model creation through relationship

**URL:** <https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860>\
**Category:** Using Django\
**Created:** [June 8, 2020, 5:43pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860 "2020-06-08T17:43:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muskua](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muskua](https://forum.djangoproject.com/u/Muskua)\
**Post date:** [June 8, 2020, 5:43pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860/1 "2020-06-08T17:43:14Z")

</div>

Hello developers.  
I got a model

```auto
class Incarico(models.Model):
        ...
        ...
        polizza = models.OneToOneField(Polizza, default=None, blank=True, on_delete=models.CASCADE, null=True)
       ...

```

and the Polizza model

```auto
class Polizza(models.Model):
	partite = models.CharField(max_length=50, default=None, blank=True, null=True) 
        ....
        ...

```

i need to create First the Incarico model through a form compiled by some users, at the moment of creation of Incarico, Polizza needs to be created and linked with the same id.  
How could i manage to do this?  
Any help is appreciated!!!

---

<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:** [June 8, 2020, 5:59pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860/2 "2020-06-08T17:59:12Z")

</div>

Override your form’s `save()` to create the `Polizza` before the `Incarico` is saved

```auto
def save(self, *args, **kwargs):
    self.instance.polizza = Polizza.objects.create(partite=self.cleaned_data["partite"])
    return super().save(*args, **kwargs)

```

---

<div class="post-metadata">

**Author:** ![Muskua](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muskua](https://forum.djangoproject.com/u/Muskua)\
**Post date:** [June 8, 2020, 7:38pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860/3 "2020-06-08T19:38:40Z")

</div>

Thanks @adamchainz!!!  
How can i save all the data inside Polizza instead? in your way it is only storing the field “partite” but i have much more on that model.  
Should i use all somewhere?

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [June 8, 2020, 7:54pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860/4 "2020-06-08T19:54:29Z")

</div>

What does your form look like? If the other Polizza fields are on the form you can update the model creation above to store those fields, as well.

---

<div class="post-metadata">

**Author:** ![Muskua](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muskua](https://forum.djangoproject.com/u/Muskua)\
**Post date:** [June 8, 2020, 7:58pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860/5 "2020-06-08T19:58:16Z")

</div>

my form is just like this:

```auto
from django.forms import ModelForm
from .models import Polizza

class PolizzaForm(ModelForm):
	class Meta:
		model = Polizza
		fields = ' __all__'

```

And the model is:

```auto
class Polizza(models.Model):
	partite = models.CharField(max_length=50, default=None, blank=True, null=True) # onetomany
	garanzie = models.CharField(max_length=50, default=None, blank=True, null=True) # onetomany
	nome_prodotto = models.CharField(max_length=50, default=None, blank=True, null=True) # testo
	modello_edizione = models.CharField(max_length=50, default=None, blank=True, null=True) # testo
	condizione_globale = models.CharField(max_length=50, default=None, blank=True, null=True) # true false
	stipula = models.CharField(max_length=50, default=None, blank=True, null=True) # date
	scadenza = models.CharField(max_length=50, default=None, blank=True, null=True) # date
	pagamento = models.CharField(max_length=50, default=None, blank=True, null=True) # date
	coassicuratrici_dirette = models.CharField(max_length=50, default=None, blank=True, null=True) # onetoone
	coassicuratrici_indirette = models.CharField(max_length=50, default=None, blank=True, null=True) # onetoone
	note = models.CharField(max_length=50, default=None, blank=True, null=True) # text

	def __str__ (self):
		return str(self.id)

```

---

<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:** [June 8, 2020, 8:00pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860/6 "2020-06-08T20:00:45Z")

</div>

Okay so using a `PolizzaForm` you want to create the `Incarico` after

```auto
def save(self, *args, **kwargs):
    polizza = super().save(*args, **kwargs)
    Incarico.objects.create(polizza=polizza, ...)
    return polizza

```

---

<div class="post-metadata">

**Author:** ![Muskua](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Muskua](https://forum.djangoproject.com/u/Muskua)\
**Post date:** [June 8, 2020, 8:02pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860/7 "2020-06-08T20:02:38Z")

</div>

last thing sry, my fault.  
Incarico models is just huge. something like 40 fields and some of them are onetoone, foreignkey and manytomany.  
It’s still that easy?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![Suttonium](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/suttonium/32/1264_2.png) [@Suttonium](https://forum.djangoproject.com/u/Suttonium)\
**Post date:** [June 8, 2020, 8:06pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860/8 "2020-06-08T20:06:01Z")

</div>

If you’re using any OneToOne, FK, or M2M fields, you will need to create the model instance separately inside the save function and then add it as a parameter. I guess you could just add the Model.objects.create() call inside the parameter too, but it will be much easier to read if you make them on their own line.

Make sure to keep your logic clear and if your model starts getting too complex there is nothing wrong with making a whole new model class to house certain information.

---

<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:** [June 8, 2020, 9:46pm UTC](https://forum.djangoproject.com/t/automatic-model-creation-through-relationship/2860/9 "2020-06-08T21:46:49Z")

</div>

> [@Muskua](#):
>
> Incarico models is just huge. something like 40 fields and some of them are onetoone, foreignkey and manytomany.  
> It’s still that easy?

Well, yes, but you might need to fill in a lot of fields if they don’t have defaults.
