Skip to content

Instantly share code, notes, and snippets.

@zhu327
Last active December 22, 2016 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhu327/c512bf581873cb709b80674bf9d6b79c to your computer and use it in GitHub Desktop.
Save zhu327/c512bf581873cb709b80674bf9d6b79c to your computer and use it in GitHub Desktop.
snippet of django orm
# 关联字段用select_related不能用正常使用only选取关联对象的某些字段,可以变通一下
a = Articles.objects.only('title').annotate(blog_name=F('blog__name')).first()
a.title
a.blog_name
# Django group by
from django.db.models import Count
Members.objects.values('designation').annotate(dcount=Count('designation'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment