Add non-null and unique field with already populated model

不问归期 提交于 2019-12-20 17:26:58

问题


I have one model in my app running in a server with a few entries. I need to add a SlugField, unique and not-null for this model. The SlugField will be populated based on trading_name. I've changed my model in order to add this new field and modified save method:

class Supplier(StatusModel):
    SLUG_MAX_LENGTH = 210
    slug = models.SlugField(unique=True, max_length=SLUG_MAX_LENGTH)
    trading_name = models.CharField(max_length=200, verbose_name=_('trading name'))
    ...

    def save(self, *args, **kwargs):
        self.slug = orig = slugify(self.trading_name)[:Supplier.SLUG_MAX_LENGTH]

        for x in itertools.count(1):
            if not Supplier.objects.filter(slug=self.slug).exists():
                break
            # Truncate the original slug dynamically. Minus 1 for the hyphen.
            self.slug = "%s-%d" % (orig[:Supplier.SLUG_MAX_LENGTH - len(str(x)) - 1], x)

        self.full_clean()
        super(Supplier, self).save(*args, **kwargs)

After changing the model, I've run manage.py makemigrations and got this migration as output:

class Migration(migrations.Migration):

    dependencies = [
        ('opti', '0003_auto_20141226_1755'),
    ]

    operations = [
        migrations.AddField(
            model_name='supplier',
            name='slug',
            field=models.SlugField(unique=True, default='', max_length=210),
            preserve_default=False,
        ),
    ]

I can't run manage.py migrate because the default value wont work due to the unique constrant.

My question is: How can I do this with Django 1.7? I need to apply the schema change and keep the current entries in my database.


回答1:


Unfortunatelly, I found no answer but I could create one solution:

  • First I have created a migration that allows the slug field to be nullable;
  • Then I have created another migration that populates the slug column with proper values to every row in the model;
  • Then another migration which adds the not-null constraint in the column.



回答2:


You do your model changes (add field, change, etc), then you call manage.py makemigrations, then apply the migrations with manage.py migrate

You can add the field with null=True, then you e.g. make a script to populate it one time

Otherwise, if you need to populate the field within the migration you can write a custom one, see https://docs.djangoproject.com/en/1.7/ref/migration-operations/#writing-your-own



来源:https://stackoverflow.com/questions/27749300/add-non-null-and-unique-field-with-already-populated-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!