[英]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.