Django 查询集在多对多字段上

发布于 2024-11-17 13:37:21 字数 1642 浏览 1 评论 0原文

我正在从事以下工作:

# 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 技术交流群。

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

发布评论

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

评论(2

迎风吟唱 2024-11-24 13:37:21

这是错误的:

FinancialProduct(1,'Company1,Company2', 'Large Product')

您保存具有多个查询的 M2M。说吧,比如:

FinancialProduct(1,'Company1', 'Large Product').save()
FinancialProduct(1,'Company2', 'Large Product').save()

This is wrong:

FinancialProduct(1,'Company1,Company2', 'Large Product')

You save an M2M with multiple queries. Say, like:

FinancialProduct(1,'Company1', 'Large Product').save()
FinancialProduct(1,'Company2', 'Large Product').save()
仲春光 2024-11-24 13:37:21

这绝对炸了我的面条(因为所有这些都非常违反直觉),但我设法想出了以下内容。我希望它可以帮助某人:

self.fields['business'].queryset = Business.objects.filter(financialproduct__id=fpID.id) 

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:

self.fields['business'].queryset = Business.objects.filter(financialproduct__id=fpID.id) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文