Ticket #21150: reproduce.py

File reproduce.py, 1.6 KB (added by Eivind Fonn <evfonn@…>, 11 years ago)

Quick executable to reproduce (assumes project and app are called bugrep)

Line 
1#!/usr/bin/env python
2
3import os
4os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bugrep.settings')
5
6import django
7from django.db.models import Count
8from bugrep.models import *
9
10Alfa.objects.all().delete()
11Bravo.objects.all().delete()
12Charlie.objects.all().delete()
13
14b = Bravo.objects.create()
15c = Charlie.objects.create(bravo=b)
16
17qsboth = Charlie.objects.select_related('alfa').annotate(Count('bravo__charlie'));
18print('select_related + annotate: %i rows' % qsboth.count())
19print(str(qsboth.query) + '\n')
20
21qsboth_rev = Charlie.objects.annotate(Count('bravo__charlie')).select_related('alfa');
22print('annotate + select_related: %i rows' % qsboth_rev.count())
23print('identical query? ' + ('yes' if str(qsboth_rev.query) == str(qsboth.query) else 'no') + '\n')
24
25qsselrel = Charlie.objects.select_related('alfa');
26print('select_related: %i rows' % qsselrel.count())
27print(str(qsselrel.query) + '\n')
28
29qsanno = Charlie.objects.annotate(Count('bravo__charlie'));
30print('annotate: %i rows' % qsanno.count() + '\n')
31
32print('Trying with non-null alfa\n')
33
34a = Alfa.objects.create()
35Charlie.objects.update(alfa=a)
36
37qsboth_nn = Charlie.objects.select_related('alfa').annotate(Count('bravo__charlie'));
38print('select_related + annotate: %i rows' % qsboth_nn.count())
39print('identical query? ' + ('yes' if str(qsboth_nn.query) == str(qsboth.query) else 'no') + '\n')
40
41qsboth_nn_rev = Charlie.objects.annotate(Count('bravo__charlie')).select_related('alfa');
42print('annotate + select_related: %i rows' % qsboth_nn_rev.count())
43print('identical query? ' + ('yes' if str(qsboth_nn_rev.query) == str(qsboth.query) else 'no') + '\n')
44
45print('Django version: ' + django.get_version())
Back to Top