第六天
程序员文章站
2022-04-05 10:28:54
...
SELECT blog_entry.*, (pub_date > '2006-01-01') FROM blog_entry;
Blog.objects.extra(
select=SortedDict([('a', '%s'), ('b', '%s')]),
select_params=('one', 'two'))
Entry.objects.extra(where=['id IN (3, 4, 5, 20)'])
q = Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"})
q = q.extra(order_by = ['-is_recent'])
Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
q = Blog.objects.aggregate(number_of_entries=Count('entry'))
{'number_of_entries': 16}
q = Blog.objects.aggregate(number_of_entries=Count('entry'))
q
umber_of_entries': 5}
'SELECT COUNT("query_entry"."id") AS "number_of_entries" FROM "query_blog"
LEFT OUTER JOIN "query_entry" ON ("query_blog"."id" = "query_entry"."blog_id")'
q = Blog.objects.annotate(Count('entry'))
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"
Blog.objects.annotate(Count('name'))
SELECT "query_blog"."id", "query_blog"."name", "query_blog"."tagline",
COUNT("query_blog"."name")
AS "name__count"
FROM "query_blog"
GROUP BY "query_blog"."id", "query_blog"."name", "query_blog"."tagline"
Entry.objects.only("headline", "body").defer("body")
Blog.objects.select_related().defer("entry__lede", "entry__body")
Entry.objects.defer("headline").filter(headline="Headline 1").defer("authors")
[<Entry_Deferred_headline: Headline 1>]
--------------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%%--------------------------
'SELECT "query_entry"."id", "query_entry"."blog_id","query_entry"."body_text", "query_entry"."pub_date", "query_entry"."n_comments",
"query_entry"."n_pingbacks", "query_entry"."rating"
FROM "query_entry"
WHERE "query_entry"."headline" = Headline 1 LIMIT 21'},
'SELECT "query_entry"."headline" FROM "query_entry"
WHERE "query_entry"."id" = 1 '
Blog.objects.in_bulk([1])注意到一个主键的值,并返回一个字典映射每个主键的值与一个对象的实例列表给定的标识。
Blog.objects.get(name__iexact='beatles blog')精确
Entry.objects.get(headline__contains='Lennon')模糊
Entry.objects.filter(id__in=[1, 3, 4])
inner_qs = Blog.objects.filter(name__contains='Cheddar')
entries = Entry.objects.filter(blog__in=inner_qs)
start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
Entry.objects.filter(pub_date__range=(start_date, end_date))
Entry.objects.filter(headline__iendswith='will')
Entry.objects.get(title__regex=r'^(An?|The) +')
b.entry_set.add(e) ###########remove(e)---------------------------区别是什么,save,add?
上一篇: 得到浏览器尺寸,判断浏览器内核