django Manytomany 和 modelform

发布于 2024-10-17 04:42:33 字数 1384 浏览 6 评论 0原文

我对 django 非常陌生,我对 django 的 M2M 如何工作以及它如何与 modelForm 或表单一起工作有一些疑问。Form 目前正在开发一个包含以下模型的应用程序:

class Agent(models.Model):
    user = models.ForeignKey(User)
    assigned_by = models.ForeignKey('self')
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return str(self.user)

class Region(models.Model):
    name = models.CharField(max_length=50)


# Agent to Region many-to-many
class ARM2M(models.Model):
    agent = models.ForeignKey(Agent)
    region = models.ForeignKey(Region)
    is_manager = models.BooleanField(default=False)

约束: 一个代理可以位于多个区域,并且他/她可以是他/她所在的任何区域的经理之一。

问题#1:是否有一种方法可以通过多对多功能来呈现这种逻辑在 Django 模型中?

-问题结束#1-

所以我有这个 AgentForm,供代理经理在他/她的区域添加新代理。但我知道这种形式不起作用:因为在 MultipleChoiceField 中,我无法获取当前登录的代理,以显示可用区域。换句话说,我被困住了。

class AgentForm(forms.Form):
    agent = forms.IntegerField(widget=forms.HiddenInput()) 
    regions = forms.MultipleChoiceField(choices=RAM2M.objects.filter(agent=agent, is_manager=True))

    def save(self):
        agent_id = self.cleaned_data['assigned_by']
        agent = Agent.objects.get(pk=int(agent_id))
        self.instance.assigned_by = agent
        super(AgentForm, self).save()

    class Meta:
        model=Agent
        exclude = ('assigned_by')

问题#2:如何为现有代理构建表单,以将新代理添加到他/她担任经理的区域。

谢谢,

马克西姆

I am really new to django, and I have some questions about how django's M2M works and also how it works with the modelForm or the forms.Form am currently working on an application that contains the following models:

class Agent(models.Model):
    user = models.ForeignKey(User)
    assigned_by = models.ForeignKey('self')
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return str(self.user)

class Region(models.Model):
    name = models.CharField(max_length=50)


# Agent to Region many-to-many
class ARM2M(models.Model):
    agent = models.ForeignKey(Agent)
    region = models.ForeignKey(Region)
    is_manager = models.BooleanField(default=False)

Constraints:
an agent can be in multiple regions, and he/she can be one of the managers in any of those regions that he/she is in.

Question#1: is there a way to present such logic with the many-to-many feature in django model?

-end-of-question#1-

So I have this AgentForm for an agent manager to add an new agent in his/her regions. But I know this form is not working: because in the MultipleChoiceField, I have no way to get the current loggedin agent, to present the available regions. In other words, I am stuck.

class AgentForm(forms.Form):
    agent = forms.IntegerField(widget=forms.HiddenInput()) 
    regions = forms.MultipleChoiceField(choices=RAM2M.objects.filter(agent=agent, is_manager=True))

    def save(self):
        agent_id = self.cleaned_data['assigned_by']
        agent = Agent.objects.get(pk=int(agent_id))
        self.instance.assigned_by = agent
        super(AgentForm, self).save()

    class Meta:
        model=Agent
        exclude = ('assigned_by')

Question#2: How do I construct a form for an existing agent to add a new agent to the regions where he/she is a manager.

Thanks,

Maxim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-10-24 04:42:33

问题#1:您可以使用 through 参数指定 M2M 关系的中间模型:(

class Agent(models.Model):
    ...
    regions = models.ManyToManyField(Region, through='ARM2M')

请参阅 http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany

关于问题#2,如果认为这有您正在寻找的答案:创建动态选择字段

Question #1: you can specify an intermediate model for the M2M relationship using the through argument:

class Agent(models.Model):
    ...
    regions = models.ManyToManyField(Region, through='ARM2M')

(See http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany)

Regarding question #2, if think that this has the answer you're looking for: Creating a dynamic choice field

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文