Django模型的值()'和LEFT OUTER连接

时间:2022-10-16 20:14:01

Consider the following models:

考虑以下模型:

class Product(models.Model):
    name = models.CharField(max_length=100, blank=False, null=False)

class Provider(models.Model):
    name = models.CharField(max_length=100, blank=False, null=False)
    product = models.ForeignKey(Product)

class Customer(models.Model):
    name = models.CharField(max_length=100, blank=False, null=False)
    product = models.ForeignKey(Product)

When I perform a query similar to:

当我执行类似于的查询时:

Product.objects.values('name', 'provider__name', 'customer__name')

The generated SQL uses LEFT OUTER join instead of INNER join. While using filter uses INNER JOIN. How can I use values() and avoid the unnecessary NULL field filtering induces by the LEFT OUTER join?

生成的SQL使用LEFT OUTER连接而不是INNER连接。使用过滤器时使用INNER JOIN。如何使用values()并避免LEFT OUTER join引起的不必要的NULL字段过滤?

Why is there a difference in filter()/values() functions behavior?

为什么filter()/ values()函数的行为存在差异?

1 个解决方案

#1


1  

you can use a filter after the values like this:

您可以在以下值之后使用过滤器:

Product.objects.values('name', 'provider__name', 'customer__name').filter(provider__name__isnull=False)

I am not 100%sure but give it a try! and lemme know!

我不是百分百肯定但是试一试!和lemme知道!

#1


1  

you can use a filter after the values like this:

您可以在以下值之后使用过滤器:

Product.objects.values('name', 'provider__name', 'customer__name').filter(provider__name__isnull=False)

I am not 100%sure but give it a try! and lemme know!

我不是百分百肯定但是试一试!和lemme知道!