Django 查询集在多对多字段上
我正在从事以下工作:
# models.py
class FinancialProduct(models.Model):
active = models.BooleanField(default=True)
businesses = models.ManyToManyField(Business)
name = models.CharField(max_length=40, unique=True)
class Item(models.Model):
main_client = models.ForeignKey(Client)
financial_product = models.ForeignKey(FinancialProduct)
advisor = models.ForeignKey(User, blank=True, null=True)
business = models.ForeignKey(Business)
class Business(models.Model):
active = models.BooleanField(default=True)
name = models.CharField(max_length=40, unique=True)
# forms.py
class ItemForm(ModelForm):
def __init__(self,fpID,*args,**kwargs):
super(ItemForm, self).__init__(*args, **kwargs)
self.fields['advisor'].queryset = User.objects.filter(groups__name='advisor')
self.fields['business'].queryset = Business.objects.filter(financialproduct__businesses=fpID)
class Meta:
model = Item
exclude = ('main_client', 'financial_product')
def CustomSave(self,f,c,u):
idb = self.save(commit=False)
idb.financial_product = f
idb.main_client = c
return idb.save()
我创建了以下数据:
Business(1,'Company1')
Business(1,'Company2')
FinancialProduct(1,'Company1', 'Small Product')
FinancialProduct(1,'Company1,Company2', 'Large Product')
在前端,我得到以下信息:
Select 'Small Product' > Get ('Company 1', 'Company 1', 'Company 2')
Select 'Large Product' > Get ('Company 1', 'Company 2')
不幸的是,我似乎以错误的方式获取数据。我在 __init__
第三行做错了什么?是因为它是M2M吗?
I am working on the following:
# models.py
class FinancialProduct(models.Model):
active = models.BooleanField(default=True)
businesses = models.ManyToManyField(Business)
name = models.CharField(max_length=40, unique=True)
class Item(models.Model):
main_client = models.ForeignKey(Client)
financial_product = models.ForeignKey(FinancialProduct)
advisor = models.ForeignKey(User, blank=True, null=True)
business = models.ForeignKey(Business)
class Business(models.Model):
active = models.BooleanField(default=True)
name = models.CharField(max_length=40, unique=True)
# forms.py
class ItemForm(ModelForm):
def __init__(self,fpID,*args,**kwargs):
super(ItemForm, self).__init__(*args, **kwargs)
self.fields['advisor'].queryset = User.objects.filter(groups__name='advisor')
self.fields['business'].queryset = Business.objects.filter(financialproduct__businesses=fpID)
class Meta:
model = Item
exclude = ('main_client', 'financial_product')
def CustomSave(self,f,c,u):
idb = self.save(commit=False)
idb.financial_product = f
idb.main_client = c
return idb.save()
And I created the following data:
Business(1,'Company1')
Business(1,'Company2')
FinancialProduct(1,'Company1', 'Small Product')
FinancialProduct(1,'Company1,Company2', 'Large Product')
In the front end I get the following:
Select 'Small Product' > Get ('Company 1', 'Company 1', 'Company 2')
Select 'Large Product' > Get ('Company 1', 'Company 2')
Unfortunately I seem to be getting the data in the wrong way. What am I doing wrong with the third line of the __init__
? Is it because it's a M2M?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是错误的:
您保存具有多个查询的 M2M。说吧,比如:
This is wrong:
You save an M2M with multiple queries. Say, like:
这绝对炸了我的面条(因为所有这些都非常违反直觉),但我设法想出了以下内容。我希望它可以帮助某人:
This absolutely fried my noodle (because all of this is highly counter intuitive) but I managed to come up with the following. I hope it helps someone: