1 | from django.db import models
|
---|
2 | from django.db.models import Q
|
---|
3 | from django.db import connection
|
---|
4 |
|
---|
5 | # Create your models here.
|
---|
6 | class Group(models.Model):
|
---|
7 | name = models.CharField(max_length=100)
|
---|
8 | activity_category = models.ForeignKey('ActivityCategory', null=True, blank=True, )
|
---|
9 |
|
---|
10 | def __str__(self, ):
|
---|
11 | return "%s (cat=%s)" % (self.name, self.activity_category_id, )
|
---|
12 |
|
---|
13 |
|
---|
14 | class ActivityCategory(models.Model):
|
---|
15 | name = models.CharField(max_length=50)
|
---|
16 |
|
---|
17 | def __str__(self, ):
|
---|
18 | return self.name
|
---|
19 |
|
---|
20 | def test():
|
---|
21 | qobj = Q(activity_category__name='Dorm') | Q(activity_category__name='Dorm')
|
---|
22 | qex = Group.objects.exclude(qobj)
|
---|
23 | print "or'd/exclude", list(qex)
|
---|
24 | queryset = Group.objects.filter(~qobj)
|
---|
25 | print "or'd/filter", list(queryset)
|
---|
26 | queryset = Group.objects.filter(~Q(qobj))
|
---|
27 | print "or'd/filterout", list(queryset)
|
---|
28 | queryset = Group.objects.filter(Q(~qobj))
|
---|
29 | print "or'd/filterin", list(queryset)
|
---|
30 |
|
---|
31 | qobj = Q(activity_category__name='Dorm')
|
---|
32 | qex = Group.objects.exclude(qobj)
|
---|
33 | print "single/exclude", list(qex)
|
---|
34 | queryset = Group.objects.filter(~qobj)
|
---|
35 | print "single/filter", list(queryset)
|
---|
36 | queryset = Group.objects.filter(~Q(qobj))
|
---|
37 | print "single/filterout", list(queryset)
|
---|
38 | queryset = Group.objects.filter(Q(~qobj))
|
---|
39 | print "single/filterin", list(queryset)
|
---|
40 |
|
---|
41 | print ""
|
---|
42 | for query in connection.queries:
|
---|
43 | print query['sql']
|
---|