Django orm model default设置但是在数据库中并没有展示

项目场景:

向模型中添加数据但是报错
django.db.utils.OperationalError: (1364, “Field ‘xxxx’ doesn’t have a default value”)


问题描述

代码中已经设置了models.IntegerField(verbose_name=‘xxx’, default=600)
但是还是报错


原因分析:

本地测试没问题线上就会出错

检查环境一致
最后发现是线上的uwsgi没有关闭重启成功所以一直是之前的错误

官方链接

default¶
Field.default¶
The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.

The default can’t be a mutable object (model instance, list, set, etc.), as a reference to the same instance of that object would be used as the default value in all new model instances. Instead, wrap the desired default in a callable. For example, if you want to specify a default dict for JSONField, use a function:

def contact_default():
return {“email”: “to1@example.com”}

contact_info = JSONField(“ContactInfo”, default=contact_default)
lambdas can’t be used for field options like default because they can’t be serialized by migrations. See that documentation for other caveats.

For fields like ForeignKey that map to model instances, defaults should be the value of the field they reference (pk unless to_field is set) instead of model instances.

The default value is used when new model instances are created and a value isn’t provided for the field. When the field is a primary key, the default is also used when the field is set to None.


解决方案:

kill -9 杀掉旧的uwsgi

总结

在模型中设置默认值,数据库中并不会设置,只会在你执行model的时候orm给你加上默认值,数据库中是没有设置默认值的,还有就是如果设置了default就不设置blank=True和null=True

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

办法总比困难多多

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值