Andrew, your solution works great for one brand. But I have more than one.

суббота, 20 мая 2017 г., 0:20:18 UTC+5 пользователь Andrew Beales написал:
>
> brand = Brand.objects.get(title='mybrand')
> products = Product.objects.filter(brand=brand).order_by('-score')[:4]
>
> ...gets you the top 4 products in order for the brand
>
> On Friday, May 19, 2017 at 10:32:49 AM UTC+1, Горобец Дмитрий wrote:
>>
>> Hello. I have these two models. I have to show 4 or less products on a page 
>> for each brand ordered by score. Please, help construct correct queryset.
>>
>>
>>
>> class Brand(models.Model):
>>     title = models.CharField(
>>         verbose_name='Название бренда',
>>         max_length=64,
>>         unique=True,
>>         db_index=True,
>>         error_messages={
>>             'unique': 'Бренд с таким именем уже существует.'
>>         }
>>     )
>>
>>     slug = AutoSlugField(
>>         verbose_name='Адрес страницы бренда',
>>         populate_from='title',
>>         editable=True,
>>         unique=True,
>>         slugify=custom_slugify,
>>         db_index=True
>>     )
>>
>>     owner = models.OneToOneField(
>>         to=settings.AUTH_USER_MODEL,
>>         verbose_name='Владелец',
>>         on_delete=models.PROTECT,
>>     )
>>
>>
>>     score = models.DecimalField(
>>         verbose_name='Рейтинг бренда',
>>         blank=True,
>>         null=True,
>>         max_digits=19,
>>         decimal_places=18,
>>     )
>>
>>
>>
>> class Product(models.Model):
>>     title = models.CharField(
>>         verbose_name='Название изделия',
>>         max_length=255,
>>         db_index=True,
>>     )
>>
>>     slug = AutoSlugField(
>>         verbose_name='Адрес страницы изделия',
>>         populate_from='title',
>>         editable=False,
>>         unique_with='brand',
>>         slugify=custom_slugify,
>>         db_index=True,
>>     )
>>
>>     brand = models.ForeignKey(
>>         to=Brand,
>>         verbose_name='Бренд',
>>         related_name='products',
>>         on_delete=models.CASCADE,
>>     )
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8d486975-7f0d-40e1-82e3-64bc5259b518%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to