Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin list_display product list

I new in django and I trying modifying a project to learn.

I've two classes in a model ('Order' and 'OrderItem'), the class OrderItem stores all items selected in a client order.

models.py

class Order(models.Model):

    STATUS_CHOICES = (
        (0, 'Waiting Payment'),
        (1, 'Completed'),
        (2, 'Canceled'),
    )

    PAYMENT_OPTION_CHOICES = (
        ('deposit', 'deposit'),
        ('paypal', 'Paypal'),
    )

    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='User')
    status = models.IntegerField(
        'Situation', choices=STATUS_CHOICES, default=0, blank=True
    )
    payment_option = models.CharField(
        'Payment Options', choices=PAYMENT_OPTION_CHOICES, max_length=20,
         default='deposit'
    )

    created = models.DateTimeField('Created in', auto_now_add=True)
    modified = models.DateTimeField('Modified in', auto_now=True)

    objects = OrderManager()

    class Meta:
        verbose_name = 'Order'
        ordering = ('-created',)

    def __str__(self):
        return 'Order #{}'.format(self.pk)

    def products(self):
        products_ids = self.items.values_list('product')
        return Product.objects.filter(pk__in=products_ids)

    def total(self):
        aggregate_queryset = self.items.aggregate(
            total = models.Sum(
                models.F('price') * models.F('quantity'),
                output_field = models.DecimalField()
            )
        )
        return aggregate_queryset['total']



class OrderItem(models.Model):

    order = models.ForeignKey(Order, verbose_name='Order',     related_name='items')
    product = models.ForeignKey('event.Product', verbose_name='Product')
    quantity = models.PositiveIntegerField('Quantity', default=1)
    price = models.DecimalField('Price', decimal_places=2, max_digits=8)

    class Meta:
        verbose_name = 'Order Item'

    def __str__(self):
        return '{}'.format(self.product)

In the django admin I can show all Orders and when I click to see more I see all products on this order, but my problem is, I can't list this products of class OrderItem in my list_display, how can I do that?

admin.py

class OrderItemInline(admin.StackedInline):
    model = OrderItem
    fields = ['product']
    readonly_fields = ['product',]
    extra = 0
    max_num = 0


class OrderAdmin(admin.ModelAdmin):

    model = Order
    inlines = [ OrderItemInline, ]

    readonly_fieldsets = (
        (None, {
            'fields': ('user','status','order','created')
        }),
    )
    readonly_fields = ['user','status','payment_option']

    search_fields = ['user__name', 'user__email']
    list_filter = ['status', ]

    list_display = ['pk','user','status','created','product']
    ordering = ('-created',)


   admin.site.register(Order, OrderAdmin)
like image 671
Hagzel Avatar asked Oct 24 '25 20:10

Hagzel


1 Answers

That's an easy task. Inside your OrderAdmin class, remove the product from the display_list list and add a string that will be the name of a method/callable, say list_products. Now, list_display will show the returned value of that function.

For example, define a list_products method inside the OrderAdmin class.

from django.utils.html import mark_safe


class OrderAdmin(admin.ModelAdmin):

    list_display = ['pk', 'user', 'status', 'created', 'list_products']

    def list_products(self, obj):
        # each obj will be an Order obj/instance/row
        to_return = '<ul>'
        # I'm assuming that there is a name field under the event.Product model. If not change accordingly.
        to_return += '\n'.join('<li>{}</li>'.format(pro_name) for prod_name in obj.items.values_list('product__name', flat=True))
        to_return += '</ul>'
        return mark_safe(to_return)
like image 94
nik_m Avatar answered Oct 26 '25 09:10

nik_m