Django在每个“makemigrations”之后创建迁移文件。因为集?

时间:2017-10-15 22:01:00

标签: python django dictionary database-migration

我在Django项目中发现了奇怪的事情。 每次运行python manage.py makemigrations命令时,都会创建名为notification的应用程序的新迁移文件。我对模型进行了 零更改 ,但是创建了新的迁移文件。我可以多次运行makemigrations命令,并创建N个迁移文件。

该模型如下所示:

from django.db import models
from django.db.models.fields import EmailField


class EmailLog(models.Model):
    email = models.EmailField(max_length=70, null=False)
    subject = models.CharField(max_length=255, null=False)
    html_body = models.TextField(null=False)
    sent_choices = {
        ('OK', 'Sent'),
        ('KO', 'Not sent'),
        ('KK', 'Unexpected problems')
    }
    status = models.CharField(max_length=2, choices=sent_choices,
                              null=False, default='KO')
    sent_log = models.TextField(null=True)
    sent_date = models.DateTimeField(auto_now_add=True, null=False)

每次迁移只会交换sent_choices字段的位置。这就是全部!

是因为sent_choices随机订单吗?我该如何避免呢?

1 个答案:

答案 0 :(得分:2)

你是对的 - 这是因为set,这是无序的。

我建议改为使用tuple

sent_choices = (
    ('OK', 'Sent'),
    ('KO', 'Not sent'),
    ('KK', 'Unexpected problems')
)

the django docs中也使用了元组。