且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态构建表单

更新时间:2023-10-02 08:58:34

感谢您的承认我的不变的正确性...



可能您需要这里是一个模型表单,它将自动从您传入的查询器构建自己:

  from django.forms.models import modelformset_factory 

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

至于动态参数, '猜测是用户,我以前使用了关闭解决方案,但是 curry 方法仍然可以工作: / p>

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

Ed评论后发表评论感谢您的澄清。我想我明白你要做什么我想知道一个内联表单可能会更好吗?如果您开始使用预先填充八个相关Team对象的Game对象,则内联表单将给您八个预先存在的表单。

  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)

是否有效?


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!

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?