[Answered ]-When queryset returned more than one, how choice specific object?

1👍

Use .filter(…) and then subscript:

x_img = ProductImages.objects.filter(product_id=x_id)[0]

You can not subscript on a .get(…) since that raises an error when multiple items are returned, hence there is no result to subscript, but a .filter(…) returns a QuerySet that you can subscript.

The above will raise an error in case there is no record to return. You can use .first() [Django-doc] to return None instead:

x_img = ProductImages.objects.filter(product_id=x_id).first()

Leave a comment