from django.db.models import Count q = Blog.objects.annotate(Count('entry')) q Blog: Blog 1>, <Blog: Foo>] q[0].name Blog 1' q[0].entry__count 3 q = Blog.objects.annotate(Avg('entry')) q[0].entry__avg 2.0 dir(q1) ['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict_ _', '__doc__', '__eq__', '__getattribute__', '__hash__', '__init__', '__metaclas s__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr_ _', '__setattr__', '__str__', '__unicode__', '__weakref__', '_base_manager', '_c ollect_sub_objects', '_default_manager', '_deferred', '_get_FIELD_display', '_ge t_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val', ' _meta', '_set_pk_val', 'delete', 'entry__count', 'entry_set', 'id', 'name', 'obj ects', 'pk', 'prepare_database_save', 'save', 'save_base', 'serializable_value', 'tagline'] q1.entry_set <django.db.models.fields.related.RelatedManager object at 0x011F0430> q1.entry_set.all() [<Entry: Headline 1>, <Entry: 2>, <Entry: for the first>] type(q1) <class 'newtest.query.models.Blog'> from django.db import connection from django.db import connection as c c.queries c.queries = [] q1.entry_set.all() [<Entry: 2>, <Entry: for the first>] >>> c.queries [{'time': '0.000', 'sql': u'SELECT "query_entry"."id", "query_entry"."blog_id", "query_entry"."headline", "query_entry"."body_text", "query_entry"."pub_date", " query_entry"."n_comments", "query_entry"."n_pingbacks", "query_entry"."rating" F ROM "query_entry" WHERE "query_entry"."blog_id" = 1 LIMIT 21'}] >>> q = Blog.objects.annotate(Count('entry')) >>> c.queries [{'time': '0.000', 'sql': u'SELECT "query_entry"."id", "query_entry"."blog_id", "query_entry"."headline", "query_entry"."body_text", "query_entry"."pub_date", " query_entry"."n_comments", "query_entry"."n_pingbacks", "query_entry"."rating" F ROM "query_entry" WHERE "query_entry"."blog_id" = 1 LIMIT 21'}] >>> q [<Blog: Blog 1>, <Blog: Foo>] >>> c.queries [{'time': '0.000', 'sql': u'SELECT "query_entry"."id", "query_entry"."blog_id", "query_entry"."headline", "query_entry"."body_text", "query_entry"."pub_date", " query_entry"."n_comments", "query_entry"."n_pingbacks", "query_entry"."rating" F ROM "query_entry" WHERE "query_entry"."blog_id" = 1 LIMIT 21'}, {'time': '0.000 ', 'sql': u'SELECT "query_blog"."id", "query_blog"."name", "query_blog"."tagline ", COUNT("query_entry"."id") AS "entry__count" FROM "query_blog" LEFT OUTER JOIN "query_entry" ON ("query_blog"."id" = "query_entry"."blog_id") GROUP BY "query_ blog"."id", "query_blog"."name", "query_blog"."tagline" LIMIT 21'}]
>>> q = Blog.objects.annotate(Avg('entry')) >>> q [<Blog: Blog 1>, <Blog: Foo>] >>> q[0].entry__avg 2.5 >>> cc.queries [{'time': '0.000', 'sql': u'SELECT "query_blog"."id", "query_blog"."name", "qu y_blog"."tagline", AVG("query_entry"."id") AS "entry__avg" FROM "query_blog" L T OUTER JOIN "query_entry" ON ("query_blog"."id" = "query_entry"."blog_id") GR P BY "query_blog"."id", "query_blog"."name", "query_blog"."tagline" LIMIT 21'} {'time': '0.000', 'sql': u'SELECT "query_blog"."id", "query_blog"."name", "que _blog"."tagline", AVG("query_entry"."id") AS "entry__avg" FROM "query_blog" LE OUTER JOIN "query_entry" ON ("query_blog"."id" = "query_entry"."blog_id") GRO BY "query_blog"."id", "query_blog"."name", "query_blog"."tagline" LIMIT 1'}]
组长一席话,胜看半天书啊,要不是点拨一下,我还真不知道要如何看下去。
因为我,看不懂。(也不全是啦)
该死的英文,它解释的再详细,可是,思路上还是理不清的乱,你说,用英语解释的,我勉强知道意思,可也感觉不太对啊。。。我想死。。。