Ticket #10673: annotation_test.diff

File annotation_test.diff, 1.3 KB (added by Ben Anhalt, 16 years ago)
  • tests/regressiontests/aggregation_regress/models.py

    diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py
    index 275ea07..35ddf9b 100644
    a b class Author(models.Model):  
    1515    def __unicode__(self):
    1616        return self.name
    1717
     18class PublisherManager(models.Manager):
     19    use_for_related_fields = True   # Line A
     20    def get_query_set(self):
     21        qs = super(PublisherManager, self).get_query_set()
     22        qs = qs.annotate(n_books=models.Count('book'))  # Line B
     23        return qs
     24
    1825class Publisher(models.Model):
    1926    name = models.CharField(max_length=300)
    2027    num_awards = models.IntegerField()
    2128
     29    objects_alternate = PublisherManager() # Line C
     30    objects = models.Manager() # Line D
     31
    2232    def __unicode__(self):
    2333        return self.name
    2434
    FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, conta  
    259269>>> Book.objects.annotate(Count('publisher')).values('publisher').count()
    2602706
    261271
     272# If the default manager adds an annotation and sets use_for_related_fields, saves can fail.
     273>>> p = Publisher.objects_alternate.all()[0]
     274>>> p.n_books
     2752
     276>>> p.save() # Failure. Commenting line A or B or swapping lines C and D, above, will yield success.
    262277"""
    263278}
    264279
Back to Top