限制Django表单中的可用选项

编程入门 行业动态 更新时间:2024-10-26 00:18:37
本文介绍了限制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表单中的可用选项

    本文发布于:2023-11-15 08:00:50,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1592604.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:表单   选项   Django

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!