Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an empty Django formset using modelformset_factory?

Tags:

django-forms

I'm creating a formset, but it seems to populate it with all of the existing data in the table for that object. I can't figure out how to start with a blank formset; the only way seems to be to delete all of the data from the table, but clearly this isn't an option.

I will post code if necessary (but there's lots of it, so knowing what is relevant is tricky).

like image 421
Nick Bolton Avatar asked Mar 14 '10 18:03

Nick Bolton


People also ask

What is inline formset in django?

Django formset allows you to edit a collection of the same forms on the same page. It basically allows you to bulk edit a collection of objects at the same time.


2 Answers

give a parameter queryset=Model.objects.none() when making the object.

like image 141
Arihant Nahata Avatar answered Oct 21 '22 10:10

Arihant Nahata


Following Arihant's answer, I did something like this, which works:

class TagCreateFormSet(BaseModelFormSet):
    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
             queryset=None, **kwargs):
        queryset = Tag.objects.none()
        super(TagCreateFormSet, self).__init__(data, files, 
            auto_id, prefix, queryset, **kwargs)
like image 25
bryanlandia Avatar answered Oct 21 '22 08:10

bryanlandia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!