django-shop icon indicating copy to clipboard operation
django-shop copied to clipboard

Ordering is ignored for product.images.all()

Open sniku opened this issue 7 years ago • 2 comments

Expected behavior: product.images.all() should be ordered based on the order column defined in the m2m-through model. Actual behavior: No ordering is applied by default.

ProductImage model which is a m2m-through model between Product and Image has order column defined, but that column is not used when making ORM queries.

>>> from myshop.all_models import *
>>> product1 = Product.objects.all()[0]
>>> product1.images.all().ordered
False
>>> str(product1.images.all().query)
'SELECT "filer_file"."id", "filer_file"."polymorphic_ctype_id", "filer_file"."folder_id", "filer_file"."file", "filer_file"."_file_size", "filer_file"."sha1", "filer_file"."has_all_mandatory_data", "filer_file"."original_filename", "filer_file"."name", "filer_file"."description", "filer_file"."owner_id", "filer_file"."uploaded_at", "filer_file"."modified_at", "filer_file"."is_public", "filer_image"."_height", "filer_image"."_width", "filer_image"."default_alt_text", "filer_image"."default_caption", "filer_image"."subject_location", "filer_image"."file_ptr_id", "filer_image"."date_taken", "filer_image"."author", "filer_image"."must_always_publish_author_credit", "filer_image"."must_always_publish_copyright" FROM "filer_image" INNER JOIN "myshop_productimage" ON ("filer_image"."file_ptr_id" = "myshop_productimage"."image_id") INNER JOIN "filer_file" ON ("filer_image"."file_ptr_id" = "filer_file"."id") WHERE "myshop_productimage"."product_id" = 1'

product1.images.all().ordered returns False. It's not clear to me why the ordering is not applied. It is specified in the Meta of the ProductImage model.

sniku avatar Dec 28 '17 11:12 sniku

I'm having the same issue.

Ayyub-Omer avatar Jul 10 '18 22:07 Ayyub-Omer

I solved this problem as follows:

product-detail.html:

{% for image in product.ordered_images %}
   ...
{% endfor %}

models.py:

class Product:
    @property
    def ordered_images(self):
        return self.images.order_by('productimage__order').all()

How to fix it more easier?

execut avatar Jan 04 '21 13:01 execut