Last active
August 29, 2015 14:01
-
-
Save hirokiky/1122956c0ca845980aa7 to your computer and use it in GitHub Desktop.
trying to get pagent having children on django 1.6.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Parent(models.Model): | |
name = models.CharField(max_length=255) | |
def __str__(self): | |
return self.name | |
class Child(models.Model): | |
parent = models.ForeignKey(Parent) | |
# Parent Child | |
# | |
# kaname --+-- child | |
# +-- child | |
# saotome (no children) | |
>>> from demoapp.models import Parent, Child | |
>>> saotome = Parent.objects.create(name='saotome') | |
>>> kaname = Parent.objects.create(name='kaname') | |
>>> Child.objects.create(parent=kaname) | |
>>> Child.objects.create(parent=kaname) | |
>>> | |
>>> | |
>>> Parent.objects.filter(child__isnull=True) | |
QUERY = 'SELECT "demoapp_parent"."id", "demoapp_parent"."name" FROM "demoapp_parent" LEFT OUTER JOIN "demoapp_child" ON ( "demoapp_parent"."id" = "demoapp_child"."parent_id" ) WHERE "demoapp_child"."id" IS NULL LIMIT 21' - PARAMS = () [0.17ms] | |
[<Parent: saotome>] | |
>>> Parent.objects.filter(child=None) | |
QUERY = 'SELECT "demoapp_parent"."id", "demoapp_parent"."name" FROM "demoapp_parent" LEFT OUTER JOIN "demoapp_child" ON ( "demoapp_parent"."id" = "demoapp_child"."parent_id" ) WHERE "demoapp_child"."id" IS NULL LIMIT 21' - PARAMS = () [0.10ms] | |
[<Parent: saotome>] | |
>>> Parent.objects.filter(child__isnull=False) | |
QUERY = 'SELECT "demoapp_parent"."id", "demoapp_parent"."name" FROM "demoapp_parent" INNER JOIN "demoapp_child" ON ( "demoapp_parent"."id" = "demoapp_child"."parent_id" ) WHERE "demoapp_child"."id" IS NOT NULL LIMIT 21' - PARAMS = () [0.15ms] | |
[<Parent: kaname>, <Parent: kaname>] | |
>>> Parent.objects.exclude(child=None) | |
QUERY = 'SELECT "demoapp_parent"."id", "demoapp_parent"."name" FROM "demoapp_parent" WHERE NOT ("demoapp_parent"."id" IN (SELECT U0."id" FROM "demoapp_parent" U0 LEFT OUTER JOIN "demoapp_child" U1 ON ( U0."id" = U1."parent_id" ) WHERE U1."id" IS NULL)) LIMIT 21' - PARAMS = () [0.20ms] | |
[<Parent: kaname>] | |
>>> Parent.objects.exclude(child__isnull=True) | |
QUERY = 'SELECT "demoapp_parent"."id", "demoapp_parent"."name" FROM "demoapp_parent" WHERE NOT ("demoapp_parent"."id" IN (SELECT U0."id" FROM "demoapp_parent" U0 LEFT OUTER JOIN "demoapp_child" U1 ON ( U0."id" = U1."parent_id" ) WHERE U1."id" IS NULL)) LIMIT 21' - PARAMS = () [0.14ms] | |
[<Parent: kaname>] | |
>>> |
I found the corresponded ticket https://code.djangoproject.com/ticket/11347
It says use distinct() lol.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
seems this line is strange a little