I have a model with dynamic choices, and I would like to return an empty choice list if I can guarantee that the code is being run in the event of a django-admin.py migrate / makemigrations
command to prevent it either creating or warning about useless choice changes.
Code:
from artist.models import Performance
from location.models import Location
def lazy_discover_foreign_id_choices():
choices = []
performances = Performance.objects.all()
choices += {performance.id: str(performance) for performance in performances}.items()
locations = Location.objects.all()
choices += {location.id: str(location) for location in locations}.items()
return choices
lazy_discover_foreign_id_choices = lazy(lazy_discover_foreign_id_choices, list)
class DiscoverEntry(Model):
foreign_id = models.PositiveIntegerField('Foreign Reference', choices=lazy_discover_foreign_id_choices(), )
So I would think if I can detect the run context in lazy_discover_foreign_id_choices
then I can choose to output an empty choice list. I was thinking about testing sys.argv
and __main__.__name__
but I'm hoping there's possibly a more reliable way or an API?
Performance
andLocation
? – Heathendommakemigrations
phase that is important here, the migration phase only executes the migration files created during it. – Jottingmigrate
command as well to avoid the warning that migrate will generate when it detects thatmakemigrations
is needed. In that sense, I guess subclassing the migrate command as per your answer would also suffice. – Turfy