Django模型中字段的默认值 [英] Default value for field in Django model
本文介绍了Django模型中字段的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个模型:
class SomeModel(models.Model):
id = models.AutoField(primary_key=True)
a = models.CharField(max_length=10)
b = models.CharField(max_length=7)
目前,我正在使用默认管理员来创建/编辑此类型的对象。
如何从管理员中删除 b
字段,以便每个对象不能使用值,而是会收到默认值 0000000
?
Currently I am using the default admin to create/edit objects of this type.
How do I remove the field b
from the admin so that each object cannot be created with a value, and rather will receive a default value of 0000000
?
推荐答案
将可编辑
设置为 False
和默认值
为默认值。
Set editable
to False
and default
to your default value.
http ://docs.djangoproject.com/en/dev/ref/models/fields/#editable
b = models.CharField(max_length=7, default='0000000', editable=False)
您的 id
字段是不必要的。 Django将自动添加。
Also, your id
field is unnecessary. Django will add it automatically.
这篇关于Django模型中字段的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文