4

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'.
  • Does this answer your question? [Django admin, foreign key field in list\_editable](https://stackoverflow.com/questions/8398797/django-admin-foreign-key-field-in-list-editable) – PyMaster Mar 04 '20 at 06:26
  • @PyMaster No it doesn't work it is for direct relation on foreign key the question i am asking is about reverse relation. – Muneer Khan Mar 04 '20 at 06:29

0 Answers0