GET products and images from two Django models, by filtering the product name and accessing the database only once

女生的网名这么多〃 提交于 2020-12-15 05:23:23

问题


I have two Django models for an eCommerce website:

class Product(models.Model):
    name=models.CharField(max_length=300)
    description=models.CharField(max_length=10000)

class Thumnbnail(models.Model):
    thumnbnail=models.ImageField(null=True)
    product=models.ForeignKey(Product, related_name='related_product', on_delete=models.CASCADE)

The user will input some keywords, and I filter on the product names with that keyword, and show only those products. With every product, on the results page, I want immediately all its product thumbnails to be loaded and shown as well.

How can I retrieve both models in the same viewset and same queryset, in an efficient way?

I know I can achieve this with two separate querysets, one being

queryset = Product.objects.filter(name__contains="Fanta").all()
return queryset

and the other viewset for the thumbnails

queryset = Product.objects.select_related('thumbnail').filter(name__contains="Fanta").all()
return queryset
# I will create another serializer to only show the thumbnails, for this specific queryset

I might not have written the last one most correctly, I am just writing pseudo-code, but I know how to do it.

My point is, I need to do the same filtering of the product_names with the input keywords twice, once to retrieve the product names and descriptions, in a ProductViewset, and one more time the same filtering, to get their thumbnails, from a ThumbnailViewset.

How do I avoid this, and do the filtering only once?

I know how .select_related() works, that I can get the both tables of both models products and thumbnails with a single database hit. But how do I show them together, and return them in a single queryset? If I return then into a single serializer, the fields name and description from Product will be repeated, as:

fields= [ product_id, product_name, product_description, thumbnail_id, thumbnail_filepath]

So for every thumbnail, the whole product_description will be repeated again and again, and as that can be 10.000 characters, if many products are shown on the page, it will be slow to transfer that data from the server to the client. And besides, I don't need the description repeated for every thumbnail. But how do I get the product description only once, with all the product thumbnails, in the most efficient way?


回答1:


Если я правильно понял вопрос, попробуйте это

# serializers.py
from rest_framework import serializers
from .models import Product


class ProductSerializer(serializers.ModelSerializer):
     thumnbnails = serializers.PrimaryKeyRelatedField(many=True, read_only=True,sourse='related_product')

     class Meta:
        model = Product
        fields = ['name', 'description', 'thumnbnails']


# views.py
...
from .serializers import ProductSerializer

....

queryset = Product.objects.filter(name__contains="Fanta").prefetch_related('related_product')
serializer = ProductSerializer(queryset, many=True)
return serializer.data

....
 


来源:https://stackoverflow.com/questions/65061179/get-products-and-images-from-two-django-models-by-filtering-the-product-name-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!