gpt4 book ai didi

Django admin,list_editable 中的外键字段

转载 作者:行者123 更新时间:2023-12-05 05:28:42 26 4
gpt4 key购买 nike

大家好 django 用户,

如何在 list_editable 管理属性中添加相关对象的字段?

# 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 类中添加一个属性,但是不行,它也不起作用。有什么线索吗?

谢谢。

最佳答案

From the documentation :

list_editable interacts with a couple of other options in particular ways; you should note the following rules:

  1. Any field in list_editable must also be in list_display. You can't edit a field that's not displayed!
  2. 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.
  3. You'll get a validation error if either of these rules are broken.

请注意,您在 list_displaylist_editable 中分别使用了 *order_reference* 和 *order__reference*。所以简而言之,我认为你不能轻易做到这一点。如果您想获得灵感,可以查看 pageadmin.py in django-cms 的实现,但这并不简单!!

关于Django admin,list_editable 中的外键字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8398797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com