AndrewIngram / django-extra-views

Django's class-based generic views are awesome, let's have more of them.
MIT License
1.38k stars 172 forks source link

limit number of forms in CreateWithInlinesView and UpdateWithInlinesView #195

Closed Bast66 closed 4 years ago

Bast66 commented 5 years ago

Hello, I have a question which I cannot find in the documentation. How to limit number of empty formsets it generates for CreateWithInlinesView and UpdateWithInlinesView. It always genereates 3 empty formsets.

I would like to have one formset per existing related entry and one extra for adding new entries.

best regards Bastian

tupps commented 5 years ago

You can limit it by passing in factory_kwargs when creating the InlineFormSetFactory.

The 'extra': 2, is the variable you want to change.


class CampaignFilterInline(InlineFormSetFactory):
    model = CampaignFilter
    fields = ['filter', 'filter_type', 'filter_join']
    factory_kwargs = {'extra': 2, 'max_num': None,
                      'can_order': False, 'can_delete': True}```
marcorichetta commented 4 years ago

I want to render only one formset and have the option to add up to 3. factory_kwargs = {'extra': 1, 'max_num': 3 ... }

How should I add the 2 extra formsets in the UI?

sdolemelipone commented 4 years ago

I'd recommend that you set extra=2 and use some javascript to hide/reveal the extra rows in the formset. That can get complicated when you're dealing with formsets containing errors in the extra rows though!

marcorichetta commented 4 years ago

@sdolemelipone Thanks for answering. I keep making some research and found this article with a pretty clear step-by-step that served at least for me.

More specifically the library django-dynamic-formset contains the JS functionality to add/remove rows.

sdolemelipone commented 4 years ago

Thanks, I wasn't aware of that jquery library. It's better than what I'd hacked together myself to deal with this!