[Answered ]-Django QuerySet Object Has No Attribute

1👍

A QuerySet s a collection of Order objects, not a single Order object, hence obj.customer makes not much sense.

The third parameter of the action is not a single item, but a QuerySet of items, you thus might want to enumerate. To boost performance, we can use .select_related(…) [Django-doc] to avoid an N+1 problem:

def order_shipped(self, request, queryset):
    for obj in queryset.select_related('customer'):
        customer_email = obj.customer.email
        if request.method == 'POST':
            send_mail(
                'Order Shipped',
                'Your order has been shipped. Please expect it within the next 3-5 days. Thank you for your business.',
                EMAIL_HOST_USER,
                [customer_email],
                fail_silently=False,
            )
    return HttpResponseRedirect('/admin/store/order')

Leave a comment