英文:
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="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 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:
<class 'stores.admin.StoreAnalytics'>: (admin.E202) 'dashboard.StoreAnatytics' has more than one ForeignKey to 'stores.Store'. You must specify a '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?
答案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='store__id', lookup_expr="exact")
store_type= add same as for type beacuse i dont know choices are integer or string
class Meta:
model = StoreAnatytics
fields = ["store_id", "store_type"]
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论