2

Hi help with values () method of queryset. When displaying related fields (Foreign key), the data is repeated, can this data be grouped?

class Product(models.Model):
    category = models.ForeignKey(Category, related_name='product', on_delete=models.CASCADE)
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique=True, db_index=True)

class ProductImage(models.Model):
    product = models.ForeignKey(Product, related_name='product_image', on_delete=models.CASCADE)
    image = models.ImageField(upload_to='img/models/')
    is_main = models.BooleanField()

View code

data = Product.objects.all().values('pk', 'title', 'product_image')

Example here

This is how it is displayed

[
    {'pk': 1, 'title': 'Product 1', 'product_image__image': 'img/models/mod_wpYzlnm.png'}, 
    {'pk': 2, 'title': 'Product 2', 'product_image__image': 'img/models/mod2_wEr0D2q.png'},
    {'pk': 2, 'title': 'Product 2', 'product_image__image': 'img/models/mod_pPQqmjB_we175uR.png'},
    {'pk': 10, 'title': 'Product 3', 'product_image__image': 'img/models/mod_3mTxkb9_z4lKV3l.png'}, 
    {'pk': 10, 'title': 'Product 3', 'product_image__image': 'img/models/heart.png'}
]

This is how it should be

[
    {'pk': 1, 'title': 'Product 1, 'product_image':[ 
        {'image':'img/models/mod_wpYzlnm.png'}
    ]}, 
    {'pk': 2, 'title': 'Product 2', 'product_image': [
        {'image':'img/models/mod2_wEr0D2q.png'}, 
        {'image':'img/models/mod_pPQqmjB_we175uR.png'}
    ]},
    {'pk': 10, 'title': 'Product 3',  'product_image': [
        {'image':'img/models/mod_3mTxkb9_z4lKV3l.png'}, 
        {'image':'img/models/heart.png'}
    ]}, 
]
6
  • When you do what query…? Commented Aug 12, 2021 at 13:42
  • data = Product.objects.all().values('pk', 'title', 'product_image') Commented Aug 12, 2021 at 13:48
  • 1
    I find it quite odd that you use a list if there are multiple items, but a string if there is one. This will make the logic of the "reader" more complicated. Commented Aug 12, 2021 at 16:40
  • Does this answer your question? Output of values() on a QuerySet with ManyToMany fields Commented Aug 13, 2021 at 7:48
  • sorry, yes there must be a list code {'pk': 1, 'title': 'Product 1, 'product_image':[ {'image':'img/models/mod_wpYzlnm.png'} ]}, Commented Aug 13, 2021 at 7:50

1 Answer 1

1

You should post-process the data with the groupby(…) function [python-doc] of the itertools module [python-doc]. For the query we should however first order on the pk, or at least an item such that the queryset ProductImages is order such that all ProductImages of the same Product are next of each other.

We can thus transform this with:

from itertools import groupby
from operator import itemgetter

data = Product.objects.values('pk', 'title', 'product_image').order_by('pk')

result = [
    {'pk': pk, 'title': title,
    'product_image': [ {'image': pi['product_image__image'] } for pi in pis ] }
    for (pk, title), pis in groupby(data, itemgetter('pk', 'title'))
]

for the given sample data, this gives us:

[{'pk': 1,
  'product_image': [{'image': 'img/models/mod_wpYzlnm.png'}],
  'title': 'Product 1'},
 {'pk': 2,
  'product_image': [{'image': 'img/models/mod2_wEr0D2q.png'},
                    {'image': 'img/models/mod_pPQqmjB_we175uR.png'}],
  'title': 'Product 2'},
 {'pk': 10,
  'product_image': [{'image': 'img/models/mod_3mTxkb9_z4lKV3l.png'},
                    {'image': 'img/models/heart.png'}],
  'title': 'Product 3'}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.