In my django project I want to save multiple instances of modal with one form, for better understanding, I have model:
class Book(models.Model):
author = models.CharField(max_length=100)
title = models.CharField(max_length=100)
languages = models.ManyToManyField(Languages, related_name='book_language')
price = models.DecimalField(max_digits=5)
In my form
class BookForm(forms.ModelForm):
multi_pricing = forms.CharField(widget=forms.HiddenInput())
pricing_type = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = Book
fields = '__all__'
If pricing_type = 'single_pricing' then do nothing and save only one model, but if value of pricing_type = 'multi_pricing'
Then I am receiving different pricing for different language in dictionary, such as -
multi_pricing = {"English":"400","Hindi":"300","French":"500"}.
What I want is to create three instance of Book and set three languages according to their price.
I don't want to use create method. What is the best and most convenient way and method which apply all validations as if user selects single_pricing?
As I overridden my form_valid, I want to save model in such a way that it goes through form_valid and clean methods before saving.
What if I can post form in url, if pricing_type = 'multi_pricing' for each language with price and pricing_type = 'single_pricing'?
form_validmethod in views and this is simple example but in my project I have 26 fields, so want to save every model as it goes thoughform_validandcleanmethod