如何限制django modelformset中外键字段的可用选项? [英] How can I limit the available choices for a foreign key field in a django modelformset?

查看:105
本文介绍了如何限制django modelformset中外键字段的可用选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

申请:
这是一个会议的研讨会提案系统。用户可以创建演示者和研讨会,并将它们链接在一起。每个用户只能访问他/他创建/拥有的演示者和研讨会。

Application: This is a workshop proposal system for a conference. A user can create presenters and workshops, and link them together. Each user should only have access to the presenters and workshops that s/he created/owns.

# Models:

class Workshop(models.Model):
    name = models.CharField(max_length=140, db_index=True)
    presenters = models.ManyToManyField("Presenter", through="WorkshopPresenter")
    owner = models.ForeignKey(User)

class Presenter(models.Model):
    name = models.CharField(max_length=140, db_index=True)
    owner = models.ForeignKey(User)

class WorkshopPresenter(models.Model):
    workshop = models.ForeignKey("Workshop")
    presenter = models.ForeignKey("Presenter")
    cardinality = models.IntegerField()

要将演示者链接到研讨会,用户被引导到一个特定于研讨会的页面,其中包含 WorkshopPresenter 的模型模型。工作坊和基数由表单填写后的视图设置,因此用户只能看到可能的演示者名称的下拉列表。
关联页面的图片

To link presenters to workshops, the user is directed to a workshop-specific page, containing a modelformset for WorkshopPresenter. Workshop and cardinality are set by the view after the formset is filled out, so the user only sees a list of dropdowns with possible presenter names. Image of the association page

问题:如何让这个关联页面上的演示者下拉列表只包含由当前用户拥有的演示者?下拉列表应该只包含 Presenter.objects.filter(owner__exact = request.user)的结果。目前,他们包含所有演示者。

Question: How can I make it so the presenter dropdowns on this association page only contain presenters who are owned by the current user? The dropdowns should only contain the results of Presenter.objects.filter(owner__exact=request.user). Currently they contain all presenters.

# View snippet that creates the formset:

workshop = Workshop.objects.filter(owner__exact=request.user).get(id=workshop_id)

MyWorkshopPresenterFormSet = modelformset_factory(WorkshopPresenter, 
                                                  formset=WorkshopPresenterFormSet, 
                                                  extra=5, 
                                                  exclude = ("workshop","cardinality"))
formset = MyWorkshopPresenterFormSet(request.POST or None,
                                     queryset=workshop.workshoppresenter_set.all())

WorkshopPresenterFormSet 只是扩展 BaseModelFormSet ,并进行一些自定义验证,没有什么好奇。

WorkshopPresenterFormSet just extends BaseModelFormSet and does some custom validation, nothing fancy.

我已经看到一些解决方案在常规表单上工作,但无法使用modelformsets。

I've seen some solutions out there that work for regular forms, but nothing to work with modelformsets.

解决方案

您可以使用功能方法(curry),闭包或回调动态更改表单上的查询集。在将自定义表单参数传递给formset的前三个答案中,查看所有三种方法/ a>。

You can dynamically change the queryset on a form using functional methods (curry), closures, or callbacks. See all three methods in the first three answers to "passing custom form parameters to formset."

另请参阅James Bennetts发帖所以你想要一个动态表单,以便对闭包方法进行深入的讨论。

Also see James Bennetts post "So you want a dynamic form" for a nice in-depth discussion of the closure method.

这篇关于如何限制django modelformset中外键字段的可用选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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