1. Making queries
2. Field lookups
Model API
创建 Model Blog, Entry, Author class Blog(models.Model): ... class Author(models.Model): ... class Entry(models.Model): headline = models.CharField(max_length=255) blog = models.ForeignKey(Blog) pub_date = models.DateTimeField() authors = models.ManyToManyField(Author) n_comments = models.IntegerField() 创建并保存新纪录 from blog.models import Entry entry = Entry(headline='some headline', ...) entry.save() 查询一个记录(Model.objects 是一个管理器) entry = Entry.objects.get(pk=1) 修改域,并保存 entry.headline="Another headline" entry.blog = Blog.objects.get(...) entry.save() 修改 ManyToManyField 并保存 entry.authors.add(Author.objects.get(...)) 无需再 save 列出所有记录 Entry.objects.all() 查询并得到一个列表 Entry.objects.filter() Entry.objects.exclude() 查询级联 Entry.objects.filter().exclude().filter() 删除一个记录,或者删除查询结果记录列表 entry = Entry.objects().get() entry.delete() entry_list = Entry.objects().filter() entry_list.delete()
查询条件
格式,注意是双下划线
field__lookuptype=value
lookuptype:
以i开头表示 case insenstive
exact, iexact 相等
contains, icontains 包含
startswith, istartswith
endswith, iendswith
in 在列表中
gt 大于
gte 大于等于
lt 小于
lte
range 在一个范围中,inclusive, []
isnull (True/False)
search (MySQL only. Full-text search, taking advantage of full-text indexing. This is like contains but is significantly faster due to full-text indexing.)
regex/iregex 正则表达式
对于 date/datetime 类型,有
year
month
day
week_day (Sunday=1, Saturday=7)
1 comments:
楼主,怎么订阅你的blog呀
Post a Comment