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
    评论
首先,你需要安装 Django:`pip install django` 接下来,你需要创建一个 Django 项目:`django-admin startproject myblog`,其 `myblog` 是项目名称。 然后,你需要在项目目录下创建一个 Django 应用:`python manage.py startapp blog`,其 `blog` 是应用名称。 接下来,我们需要配置数据库。在 `settings.py` 文件找到 `DATABASES` 设置,将其修改为: ```python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'myblog', 'USER': 'root', 'PASSWORD': '123456', 'HOST': 'localhost', 'PORT': '3306', } } ``` 这里使用了 MySQL 数据库,需要安装 PyMySQL:`pip install pymysql`,并在 `__init__.py` 文件添加以下内容: ```python import pymysql pymysql.install_as_MySQLdb() ``` 接下来,我们需要定义模型类,即 ORM 数据库映射类。打开 `blog/models.py` 文件,添加以下内容: ```python from django.db import models class Article(models.Model): title = models.CharField(max_length=128) content = models.TextField() pub_time = models.DateTimeField(auto_now_add=True) ``` 这里定义了一个 Article 类,它继承自 DjangoModel 类,用于映射到数据库的一张表。其,title、content、pub_time 三个字段分别对应表的三个列。 接下来,运行以下命令创建表: ```bash python manage.py makemigrations python manage.py migrate ``` 最后,我们可以在 Python Shell 测试 ORM 数据库映射类: ```bash python manage.py shell >>> from blog.models import Article >>> a = Article(title='Hello, Django', content='This is my first article') >>> a.save() >>> Article.objects.all() <QuerySet [<Article: Article object (1)>]> ``` 这里创建了一个 Article 对象并保存到数据库,然后使用 `objects.all()` 方法查询所有的 Article 对象。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

办法总比困难多多

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

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

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

打赏作者

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

抵扣说明:

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

余额充值