Django models: default value for column(Django 模型:列的默认值)
问题描述
我有以下 Django 模型代码:
I have following Django model code:
status = models.PositiveIntegerField(default = 0b000)
comments_allowed = models.BooleanField(default = True) # whether comments are allowed to this post
但我预计,它会生成类似的 SQL
But I expected, it would generate SQL like
`status` integer NOT NULL default '4',
`comments_allowed` bool NOT NULL default TRUE
这没有发生,当我运行 manage.py sqlall appname 它会产生:
Which is not happening and when I run manage.py sqlall appname it produces:
`status` integer UNSIGNED NOT NULL,
`comments_allowed` bool NOT NULL
深入研究 Django 的代码和谷歌搜索并没有给我什么,但 James Bennet 的评论认为 default 不会影响生成 SQL,但需要 Django 管理员.即使是这样,我如何获得预期的效果?
Delving into Django's code and googling gave me nothing, but James Bennet's comment that default is not assumed to affect generating SQL, but needed for Django admin. Even if so, how do I get desired effect?
我的 Django 版本是 1.3.0 最终版
My Django version is 1.3.0 final
推荐答案
请注意,default 参数也可以采用可调用对象:https://docs.djangoproject.com/en/dev/ref/models/fields/#default.这当然是 SQL 中无法重现的行为!因此,Django 不可能为每种可能的情况生成 SQL.看起来为了简单和一致性,他们选择不为任何情况生成 SQL.
Note that the default parameter can also take a callable object: https://docs.djangoproject.com/en/dev/ref/models/fields/#default. That is certainly a behavior that cannot be reproduced in SQL! So it would not be possible for Django to generate SQL for every possible case. It looks like for the sake of simplicity and consistency they have chosen not to generate SQL for any case.
这篇关于Django 模型:列的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django 模型:列的默认值
基础教程推荐
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
