django Manytomany 和 modelform
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题#1:您可以使用
through
参数指定 M2M 关系的中间模型:(请参阅 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:(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