Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: None  |  size: 0.99 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. What is the difference between these two Django Q-based queries:
  2. class Item(models.Model):
  3.     name = models.CharField(max_length=10)
  4.  
  5. class Relation(models.Model):
  6.     item = models.ForeignKey(Item)
  7.     weight = models.IntegerField()
  8.        
  9. some = Q(relation__x__gt=3)
  10. others = Q(relation__x=7)
  11.        
  12. first = Item.objects.filter(some, ~others)
  13.        
  14. second = Item.objects.filter(some).exclude(others)
  15.        
  16. SELECT "item_item"."id", "item_item"."name"
  17. FROM "item_item"
  18. INNER JOIN "item_relation"
  19. ON ("item_item"."id" = "item_relation"."item_id")
  20. WHERE ("item_relation"."weight" > 3  
  21.   AND NOT ("item_item"."id" IN
  22.     (SELECT U1."item_id"
  23.      FROM "item_relation" U1
  24.      WHERE (U1."weight" = 7  AND U1."item_id" IS NOT NULL))))
  25.        
  26. SELECT "item_item"."id", "item_item"."name"
  27. FROM "item_item"
  28. INNER JOIN "item_relation"
  29. ON ("item_item"."id" = "item_relation"."item_id")
  30. INNER JOIN "item_relation" T3
  31. ON ("item_item"."id" = T3."item_id")
  32. WHERE ("item_relation"."weight" > 3  AND NOT (T3."weight" = 7 ))