我无法让"list_display“从相关表中显示字段。
models.py
class product(models.Model):
product_id = models.AutoField(primary_key=True)
EAN = models.CharField(unique=True, editable=False, max_length=13)
Product_name = models.CharField(max_length=50)
class price(models.Model):
price_id = models.AutoField(primary_key=True)
EAN = models.ForeignKey(product, to_field="EAN", on_delete=models.CASCADE)
Vendor = models.ForeignKey(vendor, to_field="Vendor_name", on_delete=models.CASCADE)
Qty = models.CharField(max_length=15)
Price = models.DecimalField(max_digits=8, decimal_places=2, null=True)
panels = [
FieldPanel('EAN'),
FieldPanel('Vendor'),
FieldPanel('Qty'),
FieldPanel('Price'),
]hooks.py
class price_admin(ModelAdmin):
model = pricelist
menu_label = 'price'
menu_icon = 'pilcrow'
menu_order = 300
add_to_settings_menu = False
exclude_from_explorer = False
list_display = ('EAN_id', 'Vendor_id', 'Price') # <-Here I have a problem
list_filter = ('Vendor_id__Name',)
search_fields = ('Vendor_id__Name', 'EAN_id__EAN')我可以让"Vendor_id__Name"在"list_filter"和list_display,中工作,但是当我将"Vendor_id__Name"放到list_display,中时,我会得到以下错误:
AttributeError: Unable to lookup 'EAN_id__Product_name' on price or price_admin那么,从相关表中显示字段(在我的例子中是Vendor_id__Name)的正确方法是什么?任何帮助都将是非常感谢的!
发布于 2020-02-25 19:34:37
正如注意到的,您在相关的字段名中有一个错误。您可以使用的其他选项--方法字段或基本上--是列表显示确实接受的可调用的:
class price_admin(ModelAdmin):
...
list_display = ('vendor_name', # other fields)
def vendor_name(self, obj):
return obj.EAN.Product_name
vendor_name.short_description = 'Vendor name'https://stackoverflow.com/questions/60401818
复制相似问题