hey, I have those classes:
class CatalogImage(models.Model):
title = models.CharField(max_length=120, verbose_name=_("title"))
description = models.TextField(verbose_name=_("description"))
image = models.ImageField(verbose_name=_("image"))
class CatalogAlbum(models.Model):
title = models.CharField(max_length=120, verbose_name=_("title"))
slug = models.SlugField(max_length=120, verbose_name=_("slug"), unique=True)
images = models.ManyToManyField(to=CatalogImage, related_name='images', null=True, blank=True, through='ThroughImage')#
class ThroughImage(models.Model):
catalogImage = models.ForeignKey(CatalogImage, on_delete=models.CASCADE)
catalogAlbum = models.ForeignKey(CatalogAlbum, on_delete=models.CASCADE)
weight = models.IntegerField(verbose_name=_("weight"), unique=True)
class Meta:
ordering = ('weight',)
I use the ThroughImage
as a through
to CatalogAlbum.images
so that every CatalogImage
connected to a CatalogAlbum
has an integer for keeping order.
but I did not understand how can I order by weight
in the views.
I tried this without success:
def catalog_page(request, *args, **kwargs):
from catalogAlbum.models import CatalogAlbum
slug = kwargs['slug']
album = get_object_or_404(CatalogAlbum.objects.prefetch_related('images'), slug=slug)
if album:
album.images.order_by('throughimage__weight')
return render(request, 'catalog_page.html', {'album':album})
where is my mistake?