Building a formset dynamically

2020-06-28 15:40发布

I initially wrote code to build a form dynamically, based on data from the DB, similar to what I described in my previous SO post.

As SO user Daniel Roseman points out, he would use a formset for this, and now I've come to the realization that he must be completely right. :)

My approach works, basically, but I can't seem to get validation across the entire form to be working properly (I believe it's possible, but it's getting quite complex, and there has to be a smarter way of doing it => Formsets!).

So now my question is: How can I build a formset dynamically? Not in an AJAX way, I want each form's label to be populated with an FK value (team) from the DB.

As I have a need for passing parameters to the form, I've used this technique from a previous SO post.

With the former approach, my view code is (form code in previous link):

def render_form(request):
  teams = Team.objects.filter(game=game)
  form_collection = [] 
  for team in teams:
            f = SuggestionForm(request.POST or None, team=team, user=request.user)
            form_collection.append(f)

Now I want to do something like:

def render_form(request):
  teams = Team.objects.filter(game=game)
  from django.utils.functional import curry
  from django.forms.formsets import formset_factory

  formset = formset_factory(SuggestionForm)
  for team in teams:
      formset.form.append(staticmethod(curry(SuggestionForm, request.POST or None, team=team, user=request.user)))

But the append bit doesn't work. What's the proper way of doing this?

Thanks!

1条回答
家丑人穷心不美
2楼-- · 2020-06-28 16:24

Thanks for the recognition of my unvarying rightness...

Probably what you need here is a model formset, which will automatically build itself from a queryset you pass in:

from django.forms.models import modelformset_factory

def render_form(request):
    teams = Team.objects.filter(game=game)
    formset = modelformset_factory(form=SuggestionForm, queryset=teams)

As for the dynamic parameter, which I'm guessing is user, I've previously used the closure solution, but the curry method should still work:

    formset.form = staticmethod(curry(SuggestionForm, user=request.user))

Edit after comment Thanks for the clarification. I think I understand what you're trying to do. I wonder if an inline formset might work better? If you started off with a Game object pre-populated with eight related Team objects, an inline formset would give you eight pre-existing forms.

my_game = Game.objects.create(params=whatever)
for i in range(1, 9):
    team = Team.objects.create(game=my_game, name="team_%s" % i

formset = inlinemodelformset_factory(Game, Team, form=SuggestionForm)
fs = formset(instance=my_game)

Does that work?

查看更多
登录 后发表回答