我有一个表单,其中包含一个字段团队,该字段应该限于当前用户所属的团队。 $ b
def edit_scrapbook(request):u = request.user ScrapbookAjaxForm = modelformset_factory(剪贴簿, ('description','status','team')) choices = False 在u.team_set.all()中:如果选择:选项=选项,(t.id,t.name) else: choices = choices,(t.id,t.name)如果request.method =='POST' : formset = ScrapbookAjaxForm(request.POST, queryset = Scrapbook.objects.filter(owner = u))如果formset.is_valid(): instances = formset.save (commit = False)在实例中为 i.owner = request.user i.save() formset.save_m2m()返回HttpResponseRedirect( reverse('scrapbooks.views.index') else: formset = ScrapbookAjaxForm(queryset = Scrapbook.objects.filter(owner = u))对于表单中的表单:表单中的字段:如果field.label =='Team': field.choices = choices c = RequestContext(request) return render_to_response('scrapbooks / ajax_edit.html', {'fs':formset},context_instance = c)这似乎并不影响表单中的选择。这是非常丑陋的,可能是看这个问题的结果太久了。我也试过使用自定义表单,但我似乎无法获得自定义表单来接受参数。
如何限制Team字段的选择根据用户所在的团队,我的子集在一个表单集中?
解决方案从 django模型文档:
最后,请注意,选项可以是任何可迭代对象 - 不一定是列表或元组。这样可以动态构建选项。但是如果你发现你自己的黑客选择是的动态,你可能会更好的使用正确的数据库表与 ForeignKey。选择是为静态数据变化不大,如果有的话。
我会使用那么同样的想法:在形式上,你使用一个ForeignKey为团队,然后你可以限制该列表与一些查询。
一些进一步的建议:
I have a formset which has a field "Teams" which should be limited to the teams the current user belongs to.
def edit_scrapbook(request): u=request.user ScrapbookAjaxForm = modelformset_factory(Scrapbook, fields= ('description','status','team')) choices=False for t in u.team_set.all(): if choices: choices=choices,(t.id,t.name) else: choices=choices,(t.id,t.name) if request.method == 'POST': formset = ScrapbookAjaxForm(request.POST, queryset=Scrapbook.objects.filter(owner=u)) if formset.is_valid(): instances=formset.save(commit=False) for i in instances: i.owner=request.user i.save() formset.save_m2m() return HttpResponseRedirect(reverse('scrapbooks.views.index')) else: formset = ScrapbookAjaxForm(queryset=Scrapbook.objects.filter(owner=u)) for form in forms: for field in form: if field.label == 'Team': field.choices=choices c=RequestContext(request) return render_to_response('scrapbooks/ajax_edit.html', {'fs':formset},context_instance=c)This does not seem to affect the choices in the form at all. This is quite ugly and probably the result of looking at this problem for way too long. I have also tried using a custom formset but I can't seem to get the custom formset to accept the parameter.
How do I limit the choices for the Team field on my subselect in a formset based on the teams the user is in?
解决方案From django model documentation:
Finally, note that choices can be any iterable object -- not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices to be dynamic, you're probably better off using a proper database table with a ForeignKey. choices is meant for static data that doesn't change much, if ever.
I would use then the same idea: in the form, you use a ForeignKey for the team and then, you can limit that list with some query.
Some further suggestion:
- Use a ForeignKey for the team
- Define your own ModelChoiceField, with a query that will limit its content, basing on a parameter given in its initialization.
- Override the default field type, to use your own ModelChoiceField. Note that you should pass the filter for the team in the initialization of your ModelChoiceField.
更多推荐
限制Django表单中的可用选项
发布评论