0

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'?

2
  • Why don't you want to use the create method? Commented Jul 21, 2018 at 19:47
  • Because I overridden my form_valid method in views and this is simple example but in my project I have 26 fields, so want to save every model as it goes though form_valid and clean method Commented Jul 21, 2018 at 19:53

2 Answers 2

1

I think this code fragment will work fine. Place it in your views.py

if request.method =='POST':
            form = BookForm(request.POST)
            if form.is_valid():
                pricing_type  =form.cleaned_data['pricing_type']
                if pricing_type=="single_pricing":
                    book_object=Book()
                    book_object.author=form.cleaned_data["author"]
                    book_object.title=form.cleaned_data["title"]
                    book_object.languages=form.cleaned_data["languages"]
                    book_object.price=form.cleaned_data["price"]
                    book_object.save()
                elif pricing_type=="multi_pricing":
                    multi_pricing=form.cleaned_data['multi_pricing']
                    for each in multi_pricing.keys():
                        book_object=Book()
                        book_object.author=form.cleaned_data["author"]
                        book_object.title=form.cleaned_data["title"]
                        book_object.languages=each
                        book_object.price=multi_pricing[each]
                        book_object.save()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but what about form_valid, with it I am adding some data to model, this is an example in real I have 26 fields so, I can't do like that.
0

The create method creates an instance of the model and also saves it to the database. You can avoid using the create method, but if you want to save that object to the database, you will manually need to call save().

For example:

# 1: create instance
book_one = Book(author="Name", title="Cool Book", price=10)
# 2: save if you want to store it in the database
book_one.save()
# 3: add language object that has already been created
book_one.add(some_language_obj)

Keep in mind, that step 3 can not occur within the Book instantiation according to rules of M2M fields. Reference the docs here. Hope this is useful.

2 Comments

Thanks, but as I already mentioned that before saving instance, It should go through form_valid and clean methods. I have some custom data appending conditions in form_valid otherwise I don't have any kind of problem with create method.
You most likely have a conditional checking if the form is valid right? That calls clean() by default. So outside of the coniditional, create your Book instance. Inside your conditional, save it. Im starting to not understand what your problem is, especially without adequate code from views.py/forms.py which is the main source of the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.