Django IntegerChoices 带有可变标签

huangapple go评论52阅读模式
英文:

Django IntegerChoices with variable label

问题

I have a class that inherits from models.IntegerChoices and defines certain constants I use in my model. I want to make the "labels" of my integer choices a variable in the settings.py:

class Status(models.IntegerChoices):
    ONE = 0, settings.ONE_LABEL
    TWO = 1, settings.TWO_LABEL

When playing around with this I found that whenever I change ONE_LABEL or TWO_LABEL in my settings, Django would like to create a new migration similar to

migrations.AlterField(
    model_name='mymodel',
    name='status',
    field=models.IntegerField(choices=[(0, 'New one label'), (1, 'New two label')], default=0),
)

My goal is to deploy multiple (very similar) servers where only UI elements (that display the names of status) change according to the values in my settings. I guess it is not wise to allow changes in my settings to trigger any changes in the database, as that would make updating later versions of my project much harder. Is it safe to ignore the possible migration? I thought the way above is the easiest as I have multiple model forms and other components that use the label of my status class. This would allow a single change in my settings to be transferred throughout my project. I can't see why changing the label would require a DB migration as long as I don't change the values. I wonder if there is any better way to achieve the same goal.

英文:

I have a class that inherits from models.IntegerChoices and defines certain constants I use in my model. I want to make the "labels" of my integer choices a variable in the settings.py:

class Status(models.IntegerChoices):
    ONE = 0, settings.ONE_LABEL
    TWO = 1, settings.TWO_LABEL

When playing around with this I found that whenever I change ONE_LABEL or TWO_LABEL in my settings, Django would like to create a new migration similar to

migrations.AlterField(
    model_name='mymodel',
    name='status',
    field=models.IntegerField(choices=[(0, 'New one label'), (1, 'New two label')], default=0),
)

My goal is to deploy multiple (very similar) servers where only UI elements (that display the names of status) change according to the values in my settings.
I guess it is not wise to allow changes in my settings to trigger any changes in the database, as that would make updating later versions of my project much harder. Is it safe to ignore the possible migration?
I thought the way above is the easiest as I have multiple model forms and other components that use the label of my status class. This would allow a single change in my settings to be transferred throughout my project. I can't see why changing the label would require a DB migration as long as I don't change the values. I wonder if there is any better way to achieve the same goal.

答案1

得分: 1

Django在检测到代码中的当前模型与迁移中的模型之间存在更改时会创建新的迁移。为了避免在更改设置时创建新的迁移,您可以直接更新迁移文件,以便不会检测到任何更改:

from django.db import migrations, models
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        # 您的依赖项
    ]

    operations = [
        migrations.AlterField(
            model_name='mymodel',
            name='status',
            field=models.IntegerField(choices=[(0, settings.ONE_LABEL), (1, settings.TWO_LABEL)], default=0),
        )
    ]

英文:

Django create new migrations when it detects changes between your current models in your code and your migrations.

To avoid django from creating a new migration when you change your settings, you could update your migration file directly, so that no changes would be detected :

from django.db import migrations, models
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        # your dependencies
    ]

    operations = [
        migrations.AlterField(
            model_name='mymodel',
            name='status',
            field=models.IntegerField(choices=[(0, settings.ONE_LABEL), (1, settings.TWO_LABEL)], default=0),
)
    ]

huangapple
  • 本文由 发表于 2023年5月11日 07:13:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定