Django:在过滤期间选择匹配的外键

时间:2019-09-27 17:19:49

标签: django django-queryset

我们有以下示例模型,并且想知道是否有可能选择在同一查询期间匹配的外键(可能作为注释)。

class BaseProduct(models.Model):
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    name = models.CharField(max_length=255)
    sub_title = models.CharField(max_length=255, blank=True, null=True)
    identifier_retailer = models.CharField(max_length=255)
    tags = models.CharField(max_length=255, blank=True, null=True)
    has_multiple_variants = models.BooleanField(default=False)

class BaseProductVariant(models.Model):
    product = models.ForeignKey(BaseProduct)
    name = models.CharField(max_length=128, blank=True, null=True)
    sub_title = models.CharField(max_length=255, blank=True, null=True)
    date_created = models.DateTimeField(auto_now_add=True)
    date_updated = models.DateTimeField(auto_now=True)
    description = models.TextField(blank=True, null=True, help_text='Product description')
    features = models.TextField(blank=True, null=True, help_text='One feature per line')
    content = RedactorField(allow_image_upload=True, allow_file_upload=True, blank=True, null=True, help_text='Use this for rich HTML on featured products')
    warranty_string = models.CharField(max_length=255, blank=True, null=True)
    identifier_retailer = models.CharField(max_length=255, blank=True, null=True)
    identifier_upc = models.CharField(max_length=255, blank=True, null=True)
    identifier_model = models.CharField(max_length=255, blank=True, null=True)

我们可以使用BaseProduct.objects.filter()...轻松查询结果,但是想同时选择匹配的BaseProductVariant的列表,否则我们必须以非常规的方式查询数据库,并使用{{1 }}在prefetch_related上,BaseProductVariant.objects.filter(product__in=[]).prefetch_related(product)也可以解决此问题,但由于每行都需要反序列化,因此速度稍慢。

1 个答案:

答案 0 :(得分:1)

您可以使用prefetch_related中的BaseProduct来预取具有相关名称的变体。您还可以使用Prefetch中的django.db.models [1]对象来控制预取变体最终所处的属性名称:

from django.db.models import Prefetch

products_with_variants = BaseProduct.objects.all().prefetch_related(
    Prefetch('baseproductvariant_set', to_attr='variants'))

for p in products_with_variants:
    print(p.variants)

[1] https://docs.djangoproject.com/en/2.2/ref/models/querysets/#django.db.models.Prefetch

相关问题