来自相同模型的多个外键。

huangapple go评论97阅读模式
英文:

More than one ForeignKey from same Model

问题

I have a model Analytics

class StoreAnatytics(models.Model):
    store = models.ForeignKey(Store, on_delete=models.CASCADE, null=True)
    tax_sum = models.DecimalField(
        max_digits=10, decimal_places=2, default=0, verbose_name="tax sum"
    )

which has a ForeignKey to model Store:

class Store(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    name = models.CharField(max_length=128, blank=True, default="", verbose_name="store name")
    type = models.PositiveSmallIntegerField(choices=MARKET, default=Amazon, verbose_name="store type")

Now I need to add filters to my Analytics. The problem is that I have to filter by 2 fields: store (store pk) and type (store's type). I tried to add to my Analytics model a second ForeignKey field (type) from the Store model. But I get:

ERRORS:
<class 'stores.admin.StoreAnalytics'>: (admin.E202) 'dashboard.StoreAnatytics' has more than one ForeignKey to 'stores.Store'. You must specify an 'fk_name' attribute.
dashboard.StoreAnatytics.type: (fields.E302) Reverse accessor for 'dashboard.StoreAnatytics.type' clashes with field name 'stores.Store.type'.
HINT: Rename field 'stores.Store.type', or add/change a related_name argument to the definition for field 'dashboard.StoreAnatytics.type'.
dashboard.StoreAnatytics.type: (fields.E303) Reverse query name for 'dashboard.StoreAnatytics.type' clashes with field name 'stores.Store.type'.

My filters.py looks like:

from dashboard.models import StoreAnatytics
from django_filters import rest_framework as filters

class StoreAnatyticsFilter(filters.FilterSet):
    store = filters.CharFilter(lookup_expr="exact")

    class Meta:
        model = StoreAnatytics
        fields = ["store"]

But I need a field "type" in it. How can I solve this task?

英文:

I have a model Analytics

class StoreAnatytics(models.Model):
    store = models.ForeignKey(Store, on_delete=models.CASCADE, null=True)
    tax_sum = models.DecimalField(
        max_digits=10, decimal_places=2, default=0, verbose_name=&quot;tax sum&quot;
    )

which has a ForeignKey to model Store:

class Store(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    name = models.CharField(max_length=128, blank=True, default=&quot;&quot;, verbose_name=&quot;store name&quot;)
    type = models.PositiveSmallIntegerField(choices=MARKET, default=Amazon, verbose_name=&quot;store type&quot;)

Now I need to add a filters to my Analytics. The problem is that I have to filter by 2 fields: store (store pk) and type (store's type). I tried to add to my Analytics model a second ForeignKey field (type) from Store model. But I get:

ERRORS:
&lt;class &#39;stores.admin.StoreAnalytics&#39;&gt;: (admin.E202) &#39;dashboard.StoreAnatytics&#39; has more than one ForeignKey to &#39;stores.Store&#39;. You must specify a &#39;fk_name&#39; attribute.
dashboard.StoreAnatytics.type: (fields.E302) Reverse accessor for &#39;dashboard.StoreAnatytics.type&#39; clashes with field name &#39;stores.Store.type&#39;.
HINT: Rename field &#39;stores.Store.type&#39;, or add/change a related_name argument to the definition for field &#39;dashboard.StoreAnatytics.type&#39;.
dashboard.StoreAnatytics.type: (fields.E303) Reverse query name for &#39;dashboard.StoreAnatytics.type&#39; clashes with field name &#39;stores.Store.type&#39;.

my filters.py looks like:

from dashboard.models import StoreAnatytics
from django_filters import rest_framework as filters


class StoreAnatyticsFilter(filters.FilterSet):
    store = filters.CharFilter(lookup_expr=&quot;exact&quot;)

    class Meta:
        model = StoreAnatytics
        fields = [&quot;store&quot;]

But I need a field "type" in it. How can I solve this task?

答案1

得分: 1

class StoreAnatyticsFilter(filters.FilterSet):
    store_id = filters.NumberFilter(field_name='store__id', lookup_expr="exact")
    store_type = # 在这里添加与类型相关的筛选器,因为我不知道选项是整数还是字符串
    class Meta:
        model = StoreAnatytics
        fields = ["store_id", "store_type"]
英文:
class StoreAnatyticsFilter(filters.FilterSet):
    store_id = filters.NumberFilter(field_name=&#39;store__id&#39;, lookup_expr=&quot;exact&quot;)
    store_type= add same as for type beacuse i dont know choices are integer or string
    class Meta:
        model = StoreAnatytics
        fields = [&quot;store_id&quot;, &quot;store_type&quot;]

答案2

得分: 0

所以,问题非常简单:
我需要在模型中添加相关名称,并在admin.py中添加fk_name。

英文:

so, the problem was very easy:
I needed to add related names in model and add fk_name in admin.py.

huangapple
  • 本文由 发表于 2023年3月15日 18:03:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定