I'm building an open-source reusable app that needs users of the app to be able to set their own choices
on some CharField
fields in settings
.
Is it possible/reasonable to have Django ignore changes to the choices
field option and never (should that feature ever be added) implement database-level choice selection?
This isn't the actual code, but
This would be in models.py
class Owner(models.Model):
city = models.CharField(
verbose_name=u'City',
max_length=255,
blank=True,
choices=settings.CITIES,
db_index=True)
And this would be in settings.py
CITIES = (
('chicago', 'Chicago, IL'),
('milwaukee', 'Milwaukee, WI')
)
This would result in this migration
class Migration(migrations.Migration):
operations = [
migrations.CreateModel(
name='owner',
fields=[
('city', models.CharField(blank=True, max_length=3, db_index=True, choices=[(b'chicago', b'Chicago, IL'), (b'milwaukee', b'Milwaukee, WI')])),
]
Now, say an end-user wants to change thier app to instead have this in their settings.py
CITIES = (
('los_angeles', 'Los Angeles, CA'),
('san_fransisco', 'San Fransisco, CA')
)
This would cause another migration to be created with python manage.py makemigrations
which looks like this:
class Migration(migrations.Migration):
dependencies = [
('appname', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='user',
name='city',
field=models.CharField(blank=True, max_length=255, verbose_name='City', db_index=True, choices=[(b'los_angeles', b'Los Angeles, CA'), (b'san_fransisco', b'San Fransisco, CA')]),
),
]
Even though other users of the app may have entirely different lists of supported cities.
This could cause conflicts later when I release new versions of the open source app with a 0002
migration number, and if there is ever database-level enforcement of choices this could cause havoc.
Is it possible to have Django ignore changes to the choices
field during migration creation? Extending CharField
seems reasonable.
Or do I have to refactor this using a ForeignKey
that never changes and add select_related()
to the manager?
For reference, here's the Django makemigrations
inspector code