0

I need some advice for making a query in Django (DRF).

I'm trying to make a queryset for Products, where every Product gets a field "images" with all the Images (Files) for that product (coupled using ProductImage model), but I can't figure out how to add the field.

Below is my ProductViewSet

class ProductViewSet(viewsets.ModelViewSet):
    serializer_class = ProductSerializer
    queryset = Product.objects.all()

    def get_queryset(self): 
        queryset = Product.objects.all()
        product_list = []

        # iterate over all products
        for product in queryset:

            # find Image ids for product
            image_ids = list(ProductImage.objects.filter(product=product.id).values_list('image', flat=True))
            images = []

            # iterate over images and add to images list
            for image_id in image_ids:
                image = list(File.objects.filter(id=image_id).all())
                images.append(image)

            # add images field to product
            # product['images'] = images # "TypeError: 'Product' object does not support item assignment"
            product.images = images # Doesn't do anything.

            # add product to product list
            product_list.append(product)

        return product_list

I've tried to do the following to add the field "images":

        product['images'] = images

which gives the error "TypeError: 'Product' object does not support item assignment"

        product.images = images 

which doesn't do anything...

Could anyone point me in the right direction? Any tips on how to make this query in a better way are also welcome!

3 Answers 3

1

Does your 'Product' model have an existing 'images' field with the correct data type?

If not, you won't be able to update it with a list of FileObjects.

A better solution might be a many-to-one model relationship between 'ProductImage' and 'Product'.

Then, Django will be better able to automagically return your images through the ORM rather than complicated bespoke queries. https://docs.djangoproject.com/en/4.0/topics/db/examples/many_to_one/

Sign up to request clarification or add additional context in comments.

3 Comments

Product does not have an existing 'images' field, I want to add this field in the response of the query. I've thought about it, but I'm using the File model for other things as well, so I don't want to strictly relate it to Product. ProductImage uses a Product and a File object (where the file is an image file). Is there really no way to just add a field in the viewset? It can't be that difficult?
I've now made ProductImage its own model, with Product as foreign key. However, I still don't know how to add "images" as a field of Product in a query.
If you could share your models, it would help a lot.
0

It's hard to give an exact answer without seeing the other models, but if you write a more robust query, and use annotate, the annotated fields will be accessible on both the filtersets and serializers.

1 Comment

What would you like to know about my models? I didn't add them because I didn't think it was very relevant to my issue. I've played around with annotate but I'm not sure how it works, I can only find examples with Count(). Could you give me an example?
0

try to use a dict, product_list = {} product_list[product] = images

remove

        product.images = images # Doesn't do anything.

        # add product to product list
        product_list.append(product)

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.