Empty ModelFormset in Django FormWizard

I am using Django FormWizard. It works great, but I am having trouble displaying the correct form set form.

I have a model called Domain . I create a ModelFormset as follows:

 DomainFormset = modelformset_factory(Domain) 

I pass this to FormWizard as follows:

 BuyNowWizardView.as_view([DomainFormset]) 

I do not get any errors, but when the wizard displays the page, I get a list of all Domain objects. I want to get an empty form. How can i do this? I read that I can give a queryset parameter for ModelFormset, for example Domain.objects.none() , but it does not work as I am getting errors.

Any ideas on where I am going wrong?

thanks

+4
source share
1 answer

Django docs provide two ways to change the set of queries for a set of forms .

The first way is to pass the request as an argument when creating an instance of the form set. With formwizard you can do this by passing instance_dict

 # set the queryset for step '0' of the formset instance_dict = {'0': Domain.objects.none()} # in your url patterns url(r'^$', BuyNowWizardView.as_view([UserFormSet], instance_dict=instance_dict)), 

The second approach is to subclass BaseModelFormSet and override the __init__ method to use an empty set of queries.

 from django.forms.models import BaseModelFormSet class BaseDomainFormSet(BaseModelFormSet): def __init__(self, *args, **kwargs): super(BaseDomainFormSet, self).__init__(*args, **kwargs) self.queryset = Domain.objects.none() DomainFormSet = modelformset_factory(Domain, formset=BaseDomainFormSet) 

Then you pass the DomainFormSet to the form master, as before.

+5
source

Source: https://habr.com/ru/post/1383705/


All Articles