动态构建表单 [英] Building a formset dynamically
问题描述
由于用户丹尼尔·罗斯曼指出,他将使用一个表单,现在我来到认识到他必须完全正确。 :)
我的方法基本上是有效的,但我似乎无法在整个表单中得到验证,以便正常工作(我相信这是可能的,但它变得相当复杂,而且必须有一个更聪明的方式来做=> Formsets!)。
所以现在我的问题是:如何动态构建一个表单?不是以AJAX方式,我希望每个表单的标签都使用数据库中的FK值(团队)填充。
由于我需要将参数传递给表单,所以我使用了这个技术
使用前一种方法,我的视图代码是前一个链接中的表单代码):
def render_form(request):
teams = Team.objects.filter = game)
form_collection = []
团队成员:
f = SuggestionForm(request.POST或None,team = team,user = request.user)
form_collection.append (f)
现在我想做一些像:
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)
团队队员:
formset.form.append(staticmetho d(curry(SuggestionForm,request.POST或None,team = team,user = request.user)))
但追加位不起作用。这样做的正确方法是什么?
谢谢!
感谢您的承认我的不变的正确性...
可能您需要这里是一个模型表单,它将自动从您传入的查询器构建自己:
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?
这篇关于动态构建表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!