from django.db import models
class doctors(models.Model):
docid = models.AutoField(primary_key=True, unique=True) # Need autoincrement, unique and primary
name = models.CharField(max_length=35)
regid = models.CharField(max_length=15, default="")
photo = models.CharField(max_length=35, default="")
email = models.EmailField(default="")
phone = models.IntegerField(default=0, null=True)
qualifications = models.CharField(max_length=50, default="")
about = models.CharField(max_length=35, default="")
specialities = models.CharField(max_length=50, default="")
department = models.CharField(max_length=50, default="ENT")
fees = models.FloatField(default=300.0, null=True)
displayfee = models.IntegerField(default=0, null=True)
slotrange = models.CharField(max_length=50)
slotdurn = models.IntegerField(default=10, null=True)
breakrange = models.CharField(max_length=50, default="")
slotsleft = models.CharField(max_length=50, default="")
我的意图:如果未指定电话,费用,显示费用或slotdurn等字段之一,则应使用默认值填写。不幸的是,当我执行以下操作时,这些值会自动填充为NULL:
from appointments.models import doctors
doc=doctors(name="Dr Joel", regid="12524", email="joel@mydomain.com")
doc.save()
我检查了创建的sql,就像这样:
CREATE TABLE `appointments_doctors` (
`docid` int(11) NOT NULL,
`name` varchar(35) NOT NULL,
`regid` varchar(15) NOT NULL,
`photo` varchar(35) NOT NULL,
`email` varchar(254) NOT NULL,
`phone` int(11) DEFAULT NULL,
`qualifications` varchar(50) NOT NULL,
`about` varchar(35) NOT NULL,
`specialities` varchar(50) NOT NULL,
`department` varchar(50) NOT NULL,
`fees` double DEFAULT NULL,
`displayfee` int(11) DEFAULT NULL,
`slotrange` varchar(50) NOT NULL,
`slotdurn` int(11) DEFAULT NULL,
`breakrange` varchar(50) NOT NULL,
`slotsleft` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
似乎我指定的默认值被Null值覆盖。我添加了null,因为以前曾提示我这些字段不能为null,即使我指定了默认值也是如此。
编辑:删除浮点后,我的类如下:
class doctors(models.Model):
docid = models.AutoField(primary_key=True, unique=True) # Need autoincrement, unique and primary
name = models.CharField(max_length=35)
regid = models.CharField(max_length=15, default="")
photo = models.CharField(max_length=35, default="")
email = models.EmailField(default="")
phone = models.IntegerField(default=0)
qualifications = models.CharField(max_length=50, default="")
about = models.CharField(max_length=35, default="")
specialities = models.CharField(max_length=50, default="")
department = models.CharField(max_length=50, default="ENT")
fees = models.FloatField(default=300.0)
displayfee = models.IntegerField(default=0)
slotrange = models.CharField(max_length=50)
slotdurn = models.IntegerField(default=10)
breakrange = models.CharField(max_length=50, default="")
slotsleft = models.CharField(max_length=50, default="")
迁移脚本如下:
class Migration(migrations.Migration):
dependencies = [
('appointments', '0006_auto_20180726_0502'),
]
operations = [
migrations.AlterField(
model_name='doctors',
name='displayfee',
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name='doctors',
name='fees',
field=models.FloatField(default=300.0),
),
migrations.AlterField(
model_name='doctors',
name='phone',
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name='doctors',
name='slotdurn',
field=models.IntegerField(default=10),
),
]
但是生成的sql仍然没有默认值。如何在模型中指定这些参数并将其迁移到sql?我在python 3.6.5上使用django 2.0.7。
创建模型的正确方法是什么?