我有以下型号:
from django.db import models
from django.utils import timezone
# Create your models here.
class customer(models.Model):
# Need autoincrement, unique and primary
cstid = models.AutoField(primary_key=True, unique=True, default=1)
name = models.CharField(max_length=35)
age=models.IntegerField()
mobile=models.IntegerField()
class doctor(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="", blank=True)
photo = models.CharField(
max_length=35, default="", blank=True)
email = models.EmailField(default="")
phone = models.IntegerField(default=0, blank=True)
qualifications = models.CharField(
max_length=50, default="", blank=True)
about = models.CharField(
max_length=35, default="", blank=True)
specialities = models.CharField(
max_length=50, default="", blank=True)
department = models.CharField(max_length=50, default="ENT", blank=True)
fees = models.FloatField(default=300.0)
displayfee = models.IntegerField(default=0, blank=True)
slotrange = models.CharField(max_length=50)
slotdurn = models.IntegerField(default=10)
breakrange = models.CharField(
max_length=50, default="", blank=True)
slotsleft = models.CharField(
max_length=50, default="", blank=True)
def __str__(self):
return self.name
class appointment(models.Model):
date = models.DateField(default=timezone.now)
time = models.TimeField(default=timezone.now)
docid = models.ForeignKey(doctor, on_delete=models.CASCADE, blank=True)
cstid = models.ForeignKey(customer, on_delete=models.CASCADE, blank=True)
CstSlot = models.CharField(max_length=10)
SlotsAvailable = models.CharField(max_length=40)
Durn = models.IntegerField()
在迁移期间,出现以下错误:
You are trying to add a non-nullable field 'cstid' to appointment without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
>>>
如何解决这些问题?
答案 0 :(得分:1)
定义模型字段时。默认为null=False
。因此,如果禁止使用null,则必须设置默认值。或者,您必须设置null=True
。
答案 1 :(得分:1)
选项1
迁移时选择option 1
,并提供一些整数值,该整数值表示现有客户
选项2
通过以下任何一种方法更改您的PK
a)。添加 models.py
进行建模
null=True
b)。添加 class appointment(models.Model):
date = models.DateField(default=timezone.now)
time = models.TimeField(default=timezone.now)
docid = models.ForeignKey(doctor, on_delete=models.CASCADE, blank=True)
cstid = models.ForeignKey(customer, on_delete=models.CASCADE, blank=True, null=True)
CstSlot = models.CharField(max_length=10)
SlotsAvailable = models.CharField(max_length=40)
Durn = models.IntegerField()
进行建模
default=some_customer_id
答案 2 :(得分:0)
将您的alert("custom message");
替换为:
cstid