英文:
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),
)
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论