限制Django表单中的可用选项 [英] Limiting available choices in a Django formset

查看:550
本文介绍了限制Django表单中的可用选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中包含一个字段团队,该字段应该限于当前用户所属的团队。

$ 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为团队,然后你可以限制该列表与一些查询。



一些进一步的建议:



  • 为团队使用ForeignKey

  • 定义您自己的ModelChoiceField ,其中一个查询将限制其内容,基于初始化中给出的参数。 li>
  • 覆盖默认字段类型,以使用您自己的ModelChoiceField。请注意,您应该在ModelChoiceField的初始化过程中传递团队过滤器。


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表单中的可用选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆