django字段操作

如果更新某一条字段,或者说是少量字段用
p=Poll.ojects.get(id=1)
p.name="123"
p.save()
更新了id=1的字段,使他的名字为123.其他字段值不变。
如果想更新某条或多条记录的某几个字段,应该:
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为年
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值