如何添加到Django管理员list_editable反向关系字段

时间:2020-03-04 06:18:16

标签: python django django-models django-admin

这些是我的简单模型。

class Customer(models.Model):
     name = models.CharField(max_length=50)
     email = models.EmailField(null=True, blank=True, unique=True)
     phone = models.CharField(max_length=30, null=True, blank=True)

     def __str__(self):
         return self.name


class Box(models.Model):
     customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name='box')
     box_status = models.CharField(max_length=20)

这些是我的模型管理类。

from django.contrib import admin
from .models import Customer, Box


@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
     list_display = ['name', 'email', 'phone', 'box_status']
     list_editable = ['phone', 'box_status']

     def box_status(self, obj):
        det = list(obj.box.values_list('box_status', flat=True))
        return det

   @admin.register(Box)
   class BoxAdmin(admin.ModelAdmin):
        pass

现在,来自相关模型的box_status可以在list_display上工作,但在list_editable中不起作用。 错误是

<class 'django.forms.widgets.admin_class'>: (admin.E121) The value of 'list_editable[1]' refers to 'box_status', which is not an attribute of 'newapp.Customer'.

0 个答案:

没有答案