Hi fellow django users,
嗨其他django用户,
How can I add a field from a related object in the list_editable
admin property?
如何在list_editable admin属性中添加相关对象的字段?
# 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
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?
我试过这种方式,但它不起作用。我也尝试在Product类中添加一个属性,但是nope,它也不起作用。任何线索?
Thanks.
1 个解决方案
#1
1
从文档:
list_editable interacts with a couple of other options in particular ways; you should note the following rules:
list_editable以特定方式与其他几个选项进行交互;你应该注意以下规则:
- Any field in list_editable must also be in list_display. You can't edit a field that's not displayed!
list_editable中的任何字段也必须位于list_display中。您无法编辑未显示的字段!
- The same field can't be listed in both list_editable and list_display_links -- a field can't be both a form and a link.
list_editable和list_display_links中不能列出相同的字段 - 字段不能同时是表单和链接。
- You'll get a validation error if either of these rules are broken.
如果这些规则中的任何一个被破坏,您将收到验证错误。
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!!
请注意,您分别在list_display和list_editable中使用* order_reference *和* order__reference *。简而言之,我认为你不能轻易做到这一点。如果您想获得灵感,可以查看django-cms中pageadmin.py的实现,但这并不简单!
#1
1
从文档:
list_editable interacts with a couple of other options in particular ways; you should note the following rules:
list_editable以特定方式与其他几个选项进行交互;你应该注意以下规则:
- Any field in list_editable must also be in list_display. You can't edit a field that's not displayed!
list_editable中的任何字段也必须位于list_display中。您无法编辑未显示的字段!
- The same field can't be listed in both list_editable and list_display_links -- a field can't be both a form and a link.
list_editable和list_display_links中不能列出相同的字段 - 字段不能同时是表单和链接。
- You'll get a validation error if either of these rules are broken.
如果这些规则中的任何一个被破坏,您将收到验证错误。
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!!
请注意,您分别在list_display和list_editable中使用* order_reference *和* order__reference *。简而言之,我认为你不能轻易做到这一点。如果您想获得灵感,可以查看django-cms中pageadmin.py的实现,但这并不简单!