0

I have finally added variations to my order items so now I add items with different sizes S,M and L

When I add the variations to the admin to be displayed it comes back with

ERRORS:
<class 'core.admin.OrderItemAdmin'>: (admin.E109) The value of 'list_display[3]' must not be a ManyToManyField.

So how show i show the Size variation in the admin:

here is the models:

class VariationManager(models.Manager):
    def all(self):
        return super(VariationManager, self).filter(active=True)

    def sizes(self):
        return self.all().filter(category='size')

    def colors(self):
        return self.all().filter(category='color')


VAR_CATEGORIES = (
    ('size', 'size',),
    ('color', 'color',),
    ('package', 'package'),
)


class Variation(models.Model):
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    category = models.CharField(
        max_length=120, choices=VAR_CATEGORIES, default='size')
    title = models.CharField(max_length=120)
    image = models.ImageField(null=True, blank=True)
    price = models.FloatField(null=True, blank=True)
    objects = VariationManager()
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)

    def __str__(self):
        return self.title

class OrderItem(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE)
    ordered = models.BooleanField(default=False)
    item = models.ForeignKey(Item, on_delete=models.CASCADE)
    quantity = models.IntegerField(default=1)
    variation = models.ManyToManyField(Variation)

    def __str__(self):
        return f"{self.quantity} of {self.item.title}"

and here is the admin.py

class OrderAdmin(admin.ModelAdmin):
    list_display = ['user', 'ordered', 'ordered_date', 'coupon', 'payment', 'shipping_address', 'billing_address', 'out_for_delivery',
                    'received', 'refund_requested', 'refund_granted', 'ref_code']
    list_display_links = [
        'user',
        'shipping_address',
        'billing_address',
        'payment',
        'coupon'
    ]

class OrderItemAdmin(admin.ModelAdmin):
    list_display = ['item', 'quantity', 'ordered', ]
A_K
  • 731
  • 3
  • 15
  • 40
  • Can you share the `OrderItemAdmin`? The error is raised there – bb4L May 07 '20 at 16:33
  • @bb4L I added it, i removed the variation as it was showing this error: ```ERRORS: : (admin.E109) The value of 'list_display[3]' must not be a ManyToManyField.``` – A_K May 07 '20 at 21:30

1 Answers1

0

From Your comment i guess removing variatons from the list_display resolved the error but you want to show the content of the variations field?

I would suggest using a @property.

Take a look at the documentation or at this answer

bb4L
  • 899
  • 5
  • 16