Django admin,list_editable中的外键字段 [英] Django admin, foreign key field in list_editable

查看:81
本文介绍了Django admin,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:

  1. list_editable中的任何字段也必须在list_display中.您不能编辑未显示的字段!
  2. list_editable和list_display_links中都不能列出相同的字段-字段不能既是表单又是链接.
  3. 如果其中任何一条规则被违反,您将收到验证错误.
  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_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆