Django admin,list_editable中的外键字段 [英] Django admin, foreign key field in list_editable
问题描述
您好django用户,
Hi fellow django users,
如何在 list_editable
admin属性中从相关对象添加字段?
How can I add a field from a related object in the list_editable
admin property?
# models.py
class Order(Model):
reference = CharField(max_length=25)
class Product(Model):
name = CharField(max_length=50)
order = ForeignKey(Order)
# admin.py
class ProductAdmin:
list_display = ('name', 'order_reference')
list_editable = ('name', 'order__reference') # <--- THIS !
def order_reference(self, obj):
return obj.order.reference
我以此方式尝试过,但无法正常工作.我还尝试在 Product
类中添加一个属性,但是不,它也不起作用.有任何线索吗?
I tried it this way, but it won't work. I also tried to add a property in the Product
class, but nope, it won't work either. Any clue?
谢谢.
推荐答案
list_editable与其他几个特别的选项交互方法;您应该注意以下规则:
list_editable interacts with a couple of other options in particular
ways; you should note the following rules: 请注意,您分别在 list_display 和 list_editable 中使用* order_reference *和* order__reference *.简而言之,我不认为您可以轻松做到这一点.如果您想获得灵感,可以查看 pageadmin.py in django-cms ,但这并不简单! Notice that you use *order_reference* and *order__reference* in list_display and list_editable, respectively. So in short, I don't think you can do this easily. If you want to have inspiration, you could check out the implementation of pageadmin.py in django-cms, but it's NOT straightforward!! 这篇关于Django admin,list_editable中的外键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!