| 38 | # Models for 11890 -- do not change since bug includes a really |
| 39 | # esoteric edge case |
| 40 | class Person(models.Model): |
| 41 | first_name = models.CharField(max_length=200) |
| 42 | last_name = models.CharField(max_length=50) |
| 43 | best_friend = models.ForeignKey('self', blank=True, null=True, editable=False) |
| 44 | |
| 45 | nick_name = models.CharField(max_length=50, blank=True, null=True) |
| 46 | |
| 47 | class Candy(models.Model): |
| 48 | owner = models.ForeignKey(Person, related_name="candies") |
| 49 | qty = models.PositiveIntegerField(default=0) |
| 50 | |
| 51 | |
145 | | [<class 'regressiontests.defer_regress.models.Child'>, <class 'regressiontests.defer_regress.models.Item'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name_other_value_text'>, <class 'regressiontests.defer_regress.models.Item_Deferred_other_value_text_value'>, <class 'regressiontests.defer_regress.models.Item_Deferred_text_value'>, <class 'regressiontests.defer_regress.models.Leaf'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_name_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_second_child_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_value'>, <class 'regressiontests.defer_regress.models.RelatedItem'>, <class 'regressiontests.defer_regress.models.RelatedItem_Deferred_item_id'>, <class 'regressiontests.defer_regress.models.ResolveThis'>, <class 'regressiontests.defer_regress.models.ResolveThis_Deferred_num'>] |
| 159 | [<class 'regressiontests.defer_regress.models.Candy'>, <class 'regressiontests.defer_regress.models.Child'>, <class 'regressiontests.defer_regress.models.Item'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name'>, <class 'regressiontests.defer_regress.models.Item_Deferred_name_other_value_text'>, <class 'regressiontests.defer_regress.models.Item_Deferred_other_value_text_value'>, <class 'regressiontests.defer_regress.models.Item_Deferred_text_value'>, <class 'regressiontests.defer_regress.models.Leaf'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_name_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_second_child_value'>, <class 'regressiontests.defer_regress.models.Leaf_Deferred_value'>, <class 'regressiontests.defer_regress.models.Person'>, <class 'regressiontests.defer_regress.models.RelatedItem'>, <class 'regressiontests.defer_regress.models.RelatedItem_Deferred_item_id'>, <class 'regressiontests.defer_regress.models.ResolveThis'>, <class 'regressiontests.defer_regress.models.ResolveThis_Deferred_num'>] |
| 161 | # Regression for #11890 - Defer/only + annotate is broken |
| 162 | >>> from django.db.models import Count, Avg |
| 163 | >>> Item.objects.all().delete() |
| 164 | >>> RelatedItem.objects.all().delete() |
| 165 | >>> i = Item.objects.create(name='Bug #11890', value=1) |
| 166 | >>> RelatedItem.objects.create(item=i) |
| 167 | <RelatedItem: RelatedItem object> |
| 168 | >>> RelatedItem.objects.create(item=i) |
| 169 | <RelatedItem: RelatedItem object> |
| 170 | >>> counted = Item.objects.only('name').annotate(Count('relateditem')) |
| 171 | >>> counted.count() |
| 172 | 1 |
| 173 | >>> counted[0].relateditem__count |
| 174 | 2 |
| 175 | |
| 176 | # Adding up some spice since result row index offset is affected by extra selections |
| 177 | >>> counted = Item.objects.extra(select={'past': 'now()'}).annotate(rel_count=Count('relateditem'), avg_id=Avg('id')).extra(select={'sometime': 'now()'}) |
| 178 | >>> counted.count() |
| 179 | 1 |
| 180 | >>> counted[0].rel_count |
| 181 | 2 |
| 182 | |
| 183 | # Rather interestingly, all those select_related and defer are required to |
| 184 | # trigger some edge case behaviour |
| 185 | >>> from django.db.models import Sum; |
| 186 | >>> Person.objects.all().delete() |
| 187 | >>> Candy.objects.all().delete() |
| 188 | >>> p = Person.objects.create(first_name='Mr', last_name='Egotistic') |
| 189 | >>> p.best_friend = p |
| 190 | >>> p.id = 1 # For determinism |
| 191 | >>> p.save() |
| 192 | >>> creations = [Candy(pk=n, owner_id=p.pk, qty=42).save() for n in xrange(10)] |
| 193 | >>> Candy.objects.aggregate(Sum('qty'))['qty__sum'] |
| 194 | 378 |
| 195 | >>> Person.objects.annotate(Sum('candies__qty')).select_related('best_friend').defer('first_name','best_friend__nick_name').get(pk=p.pk).candies__qty__sum |
| 196 | 378 |