社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

如何在django管理列表中添加可编辑的反向关系字段

Muneer Khan • 4 年前 • 206 次点击  

这些是我的简单模型。

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

现在,来自相关模型的框状态可以显示工作列表,但不能在列表中工作可编辑。 错误是

<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'.
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56933
 
206 次点击