Automatic model creation through relationship

Hello developers.
I got a model

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

and the Polizza model

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!!!

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

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

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?

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.

my form is just like this:

from django.forms import ModelForm
from .models import Polizza

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

And the model is:

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)

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

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

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

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.

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