how to add to django admin list_editable a reverse relation Field

Viewed 500

These are my simple models.

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)

these are my model admin classes.

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

Now box_status from related model can work list_display but not working in list_editable. the error is

<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 Answers
Related