Django模型:列的默认值 [英] Django models: default value for column

查看:89
本文介绍了Django模型:列的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Django 型号代码:

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 的代码和谷歌搜索给了我什么,但詹姆斯·班纳特的评论说,默认不会影响生成 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 final

My Django version is 1.3.0 final

推荐答案

请注意,默认值参数也可以使用可调用对象: https://docs.djangoproject.com/en/dev/ref/models/fields /#默认。这当然是一个不能在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模型:列的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆