运行迁移以覆盖保存的新添加字段

时间:2019-03-11 16:08:08

标签: django python-3.x django-migrations

我一直在运行诊所管理软件,到目前为止,我正在通过输入患者的详细信息(包括年龄和月份)来对患者进行注册。这存储在两个IntegerFields中。但是我意识到,如果明年有相同的患者就诊,他的年龄仍将显示相同。因此,我需要创建一个大概的出生日期(我不要求患者在注册时给出出生日期)。

因此,我实现了一个函数,该函数可以计算当前年龄的大概出生日期。我否决了save方法,以便如果患者不输入出生日期,则会在数据库中插入一个计算日期。

我的问题是为现有客户创建此文件。使用新的DateField和save方法更新模型后,在尝试运行makemigrations时,系统会提示我输入默认值,而且我不知道如何使makemigrations使用我的函数来更新现有记录耐心。我是否只是将字段设置为blank和null tr​​ue,然后运行自定义方法来检查该字段是否为空白,并更新该字段的数据?还是可以让makemigrations本身运行我实现的功能?正确的方法是什么?

我的模型和方法:

class customer(models.Model):
    # Need autoincrement, unique and primary
    cstid = models.AutoField(primary_key=True, unique=True)
    name = models.CharField(max_length=35)
    ageyrs=models.IntegerField(blank=True)
    agemnths=models.IntegerField(blank=True)
    dob = models.DateField()
    gender_choices = (
        ('male', 'Male'),
        ('female', 'Female'),
        ('other', 'Something else'),
        ('decline', 'Decline to answer')
        )
    gender = models.CharField(
        choices=gender_choices, max_length=10, default='male')
    maritalstatus_choices = (
        ('unmarried', 'Unmarried'),
        ('married', 'Married')
                            )
    maritalstatus = models.CharField(
        choices=maritalstatus_choices, max_length=10, default='Unmarried')
    mobile = models.CharField(max_length=15, default='')
    alternate = models.CharField(max_length=15, default='', blank=True)
    email = models.CharField(max_length=50, default='', blank=True)
    address = models.CharField(max_length=80, default='', blank=True)
    city = models.CharField(max_length=25, default='', blank=True)
    occupation = models.CharField(max_length=25, default='', blank=True)
    bloodgroup_choices = (('apos', 'A+'),
        ('aneg', 'A-'),
        ('bpos', 'B+'),
        ('bneg', 'B-'),
        ('opos', 'O+'),
        ('oneg', 'O-'),
        ('abpos', 'AB+'),
        ('abneg', 'AB-')
        )
    bloodgroup = models.CharField(choices=bloodgroup_choices, max_length=5, default='-', blank=True)
    linkedclinic = models.ForeignKey(Clinic, on_delete=models.CASCADE)

    class Meta:
        unique_together = ["name", "mobile", "linkedclinic"]


    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        if not self.dob:
            current = datetime.datetime.now()
            newdate = current - relativedelta(years=self.ageyrs, months=self.agemnths)
            if not self.ageyrs == 0:
    #           datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
                dob = datetime.datetime(newdate.year, 1, 1)
            else:
                print("Age is less than 1 year")
                dob = newdate
            self.dob = dob
        super().save(*args, **kwargs)  # Call the "real" save() method.

    def age(self):
        if self.ageyrs == 0 and self.agemnths == 0:
            return "0yr"
        if self.ageyrs == 0:
            return str(self.agemnths) + "m"
        if self.agemnths == 0:
            return str(self.ageyrs) +"yr"
        return str(self.ageyrs) +"yr " +  str(self.agemnths) + "m"

在进行makemigrations时:

joel@hp:~/myappointments$ ./manage.py makemigrations
You are trying to add a non-nullable field 'dob' to customer 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 个答案:

答案 0 :(得分:2)

通常,我使用三个迁移来处理此类模型更改:

  1. 使用null=True添加新字段
  2. 填充空字段
  3. 将新添加的字段更改为null=False

第二次迁移可以使用RunSQLRunPython,例如看起来像这样:

def forward(apps, schema_editor):
    customer = apps.get_model("<app_name>", "customer")
    db_alias = schema_editor.connection.alias
    current = datetime.datetime.now()
    for c in customer.objects.using(db_alias).all():
        newdate = current - relativedelta(years=c.ageyrs, months=c.agemnths)
        if not self.ageyrs == 0:
            dob = datetime.datetime(newdate.year, 1, 1)
        else:
            print("Age is less than 1 year")
        dob = newdate
        c.dob = dob.date()
        c.save()


def rollback(apps, schema_editor):
    pass


...
    operations = [migrations.RunPython(forward, rollback)]

您可以使用python manage.py makemigrations <app_name> --empty生成它,然后使用适当的数据对其进行修改。当您要创建第三个迁移时,在您发布的提示中,将出现第三个选项,例如“我在以前的迁移中使用手动方式处理了此问题”。

相关问题