简体   繁体   中英

Django models 'values()' and LEFT OUTER join

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?

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM