1 | from django.db import models
|
---|
2 |
|
---|
3 | class ModelA(models.Model):
|
---|
4 | name = models.CharField(max_length=100)
|
---|
5 |
|
---|
6 | def __unicode__(self):
|
---|
7 | return self.name
|
---|
8 |
|
---|
9 | class ModelB(models.Model):
|
---|
10 | name = models.CharField(max_length=100)
|
---|
11 | somebool = models.BooleanField(default=True)
|
---|
12 | a = models.ForeignKey(ModelA)
|
---|
13 |
|
---|
14 | def __unicode__(self):
|
---|
15 | return self.name
|
---|
16 |
|
---|
17 | from django.db.models.aggregates import Count
|
---|
18 | __test__ = {"doctest": """
|
---|
19 | >>> a = ModelA.objects.create(name="testA")
|
---|
20 | >>> b1 = ModelB.objects.create(name="B1", a=a, somebool=True)
|
---|
21 | >>> b2 = ModelB.objects.create(name="B2", a=a, somebool=True)
|
---|
22 | >>> ModelA.objects.all().annotate(num_b=Count('modelb')).filter(modelb__somebool=True)[0].num_b
|
---|
23 | 2
|
---|
24 | """}
|
---|