【问题标题】:Nested select_related to get data from various models django ORM嵌套 select_related 从各种模型 django ORM 中获取数据
【发布时间】:2021-09-17 19:46:13
【问题描述】:

我有以下 5 个模型:

class Products:
    product_name=models.CharField(max_length=300,blank=True)
    product_price=models.IntegerField(blank=True)

class ProductImages(models.Model):
    image=models.ImageField(upload_to='product_images',null=True,blank=True)
    image_product=models.ForeignKey(Product,on_delete=models.CASCADE)

class ProductModel(models.Model):
    model_product=models.ForeignKey(Product,null=True,on_delete=models.CASCADE)
    model_name=models.CharField(max_length=130,blank=True)

class Colour(models.Model):
    colour_name=models.CharField(max_length=20,blank=True)
    colour_product=models.ForeignKey(Product,on_delete=models.CASCADE)

class Sale:
     name=models.CharField(max_length=30,null=True,blank=True)

class ProductSale:
    saleid=models.ForeignKey(Sale,on_delete=models.CASCADE)
    product=models.ForeignKey(Product,on_delete=models.CASCADE)

我正在根据销售 ID 过滤并获取 ProductSale 中的产品

prod=ProductSale.objects.filter(sale=sale.id).prefetch_related('product')

现在我有产品 ID,但我还想要更多详细信息,即产品图像、颜色、ProductImages 模型中存在的模型、颜色模型、ProductModel 模型。我怎样才能以最优化的方式实现这一点,下面是我的尝试,但是它需要时间并且没有优化。

prod=ProductSale.objects.filter(sale=sale.id).prefetch_related('product')

for prod in prod:
    pm=ProductModel.objects.filter(model_product=prod.product_id).values()
    pc=Colour.objects.filter(colour_product=prod.product_id).values()
    images=prod.product.productimages_set.all().order_by('id')[:2].values()

    sale_product.append({
        "product_id": prod.product.id,
        "product_name": prod.product.product_name,
        "stock": prod.product.product_quantity,
        "product_price": prod.product.product_price,
        "sale_price": prod.product.sale_price,
        "saleprice_startdate": prod.product.saleprice_startdate,
        "saleprice_enddate": prod.product.saleprice_enddate,
        "product_category": prod.product.category_name,
        "product_reviews": review_product,
        "review_count": len(pr),
        "product_images": images,
        "product_colour": pc,
        "product_model": pm,
    })

如何优化查询以我在字典中使用的格式获取数据,请帮助陷入困境。

【问题讨论】:

  • 你为什么使用prod in prodpr 是什么? review_product 是什么?
  • pcpm 也会有列表作为结果。这符合你的预期吗?
  • 我在 prod 中使用 prod,循环浏览产品并针对产品的每个 id 查找颜色(pc)、产品型号(pm),Pr 是产品评论,这也是我得到的为了避免代码太多,这里就不提了,方便大家理解

标签: django django-models django-queryset django-orm django-select-related


【解决方案1】:

试一试:

prod = ProductSale.objects.select_related(
    'product',
).prefetch_related(
    'product__colour',
    'product__productreview',
    Prefetch('product__productimages', queryset=ProductImages.objects.order_by('id'))),
).filter(sale=sale.id)

for p in prod:
    pm = p.product
    pc = p.colour.all()
    images = p.productimages.all()[:2]
    review_product = p.productreview.all()

    sale_product.append({
        "product_id": p.product.id,
        "product_name": p.product.product_name,
        "stock": p.product.product_quantity,
        "product_price": p.product.product_price,
        "sale_price": p.product.sale_price,
        "saleprice_startdate": p.product.saleprice_startdate,
        "saleprice_enddate": p.product.saleprice_enddate,
        "product_category": p.product.category_name,
        "product_reviews": review_product,
        "review_count": len(review_product),
        "product_images": images,
        "product_colour": pc,
        "product_model": pm,
    })

【讨论】:

  • 好的,谢谢,让我试试这个,我会回复你的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
  • 2020-03-21
  • 2020-09-19
  • 1970-01-01
  • 2017-05-20
  • 1970-01-01
相关资源
最近更新 更多