简体   繁体   English

列的Django DB级默认值

[英]Django DB level default value for a column

Django is proving the model field argument default ( https://docs.djangoproject.com/en/dev/ref/models/fields/#default ) but as I know it will be called every time new object is created via django. Django证明了模型字段参数为默认值( https://docs.djangoproject.com/en/dev/ref/models/fields/#default ),但据我所知,每次通过django创建新对象时都会调用它。 If we insert/create a records with raw queries (using django.db.connection.cursor ) we will get the exception cause Field 'xyz' doesn't have a default value. 如果我们使用原始查询(使用django.db.connection.cursor )插入/创建记录,则会得到异常,因为字段“ xyz”没有默认值。

How to represent the db level default value for the column in model. 如何表示模型中列的数据库级别默认值。 Like db_index. 就像db_index。

I hope you guy understand my question. 我希望你能理解我的问题。

There is an open ticket 470 to include default values in the SQL schema. 存在开放票据470以在SQL模式中包括默认值。

Until this feature has been added to Django, you'll have to manually run alter table statements yourself or write a migration to run them if you want a default value in the SQL schema. 在Django中添加此功能之前,如果要在SQL模式中使用默认值,则必须自己手动运行alter table语句或编写迁移以运行它们。

Note that Django allows callables as defaults, so even if this feature is added to Django, it won't be possible to have all defaults in the database. 请注意,Django允许可调用对象作为默认值,因此即使将此功能添加到Django,也无法在数据库中拥有所有默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django默认隔离级别和db二进制日志格式错误 - Django default isolation level and db binary log format error 如何使用django 1.7.3 / postgres迁移在db中设置默认列值? - How to set default column value in the db using django 1.7.3/postgres migrations? django的低级缓存中的默认值如何工作? - How does the default value in django's low level cache work? Django默认放置空字符串,但在DB列中将其设置为null - Django is putting empty string by default while excepting it to be null in DB column 自定义用户字段的 Django 默认值未添加到数据库表中 - Django Default value for a custom user field is not added into the db tables django.db.utils.IntegrityError:列中的值为空-但值不为空 - django.db.utils.IntegrityError: null value in column - but value is not null 为什么列的默认值不适用于Django - Why default value for column doesn't work with django Django:将 model 的 pk 设置为其列中的默认值 - Django: Set pk of a model as default value in its column Django:检测模块级数据库查询 - Django: detect module level db queries django.db.utils.ProgrammingError: 列“问题”的类型是字符变化 [] 但默认表达式是整数类型 - django.db.utils.ProgrammingError: column “questions” is of type character varying[] but default expression is of type integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM