I have a django model with a field that uses a enum for it’s choices tuple like this:
<code>VERSION_CHOICES = tuple(
(v.value, v.value) for v in ForwardsUpdateEventSensitivityVersion
)
version = models.CharField(
max_length=max(len(s[0]) for s in VERSION_CHOICES),
choices=VERSION_CHOICES,
)
</code>
<code>VERSION_CHOICES = tuple(
(v.value, v.value) for v in ForwardsUpdateEventSensitivityVersion
)
version = models.CharField(
max_length=max(len(s[0]) for s in VERSION_CHOICES),
choices=VERSION_CHOICES,
)
</code>
VERSION_CHOICES = tuple(
(v.value, v.value) for v in ForwardsUpdateEventSensitivityVersion
)
version = models.CharField(
max_length=max(len(s[0]) for s in VERSION_CHOICES),
choices=VERSION_CHOICES,
)
What is the best practice for writing the accompanying migration?
Using the enum directly like this:
<code>models.CharField(
choices=[
tuple(
(v.value, v.value)
for v in ForwardsUpdateEventSensitivityVersion
)
],
)
</code>
<code>models.CharField(
choices=[
tuple(
(v.value, v.value)
for v in ForwardsUpdateEventSensitivityVersion
)
],
)
</code>
models.CharField(
choices=[
tuple(
(v.value, v.value)
for v in ForwardsUpdateEventSensitivityVersion
)
],
)
Or hardcoding the values like this:
<code>models.CharField(
choices=[("V1", "V1"), ("V2", "V2")],
)
</code>
<code>models.CharField(
choices=[("V1", "V1"), ("V2", "V2")],
)
</code>
models.CharField(
choices=[("V1", "V1"), ("V2", "V2")],
)
2
Hardcoding the values is preferable, because then the migration is a self-contained script and doesn’t have a library dependency to import.
Importing application code during a migration can be problematic, because the code (and most importantly the models) may have shifted.