如果更新某一条字段,或者说是少量字段用
p=Poll.ojects.get(id=1)
p.name="123"
p.save()
更新了id=1的字段,使他的名字为123.其他字段值不变。
如果想更新某条或多条记录的某几个字段,应该:
Poll.object.filter(过滤条件).update(字段=值,字段=值,...)
返回值就是影响的记录数量
Poll.object.filter(过滤条件).update(字段=值,字段=值,...)
返回值就是影响的记录数量
p=Poll.ojects.filter(banji="09-2").update(beizhu="null",sex="nluu")
p.save()
>>> Poll.objects.filter(id=1)
[<Poll: What's up?>]
>>> Poll.objects.filter(question__startswith='What')
[<Poll: What's up?>]
# Get the poll whose year is 2012.
>>> Poll.objects.get(pub_date__year=2012)
<Poll: What's up?>
__startswith双下线和startswith在id=1条件下,字段question开头为what的被过滤出来
__year=2012则判断的时间为2012的被过滤下来
>>> p =
Poll.objects.get(pk=1)
>>> p.was_published_recently()
# Display any choices from the related object set -- none so far
.>>> p.choice_set.all()
中pk代表主键,检索主键为1的字段,
显示最新记录,结果为true或者false
p.choice_set.all()
选择显示任何从相关的对象集
choice_set.all()可以再这里参考
https://docs.djangoproject.com/en/1.4/ref/models/querysets/
p.choice_set.count()
查询总记录数目
p.choice_set.create(choice='The sky',
votes=0)
创建相关对象集
Choice.objects.filter(poll__pub_date__year=2012)
poll对象
pub_date poll的字段
year为年