2đź‘Ť
You could simply define the model without any conditionals and tweak your app logic so that instances of ConsumerRegistration
model are only interacted with (i.e. created, updated etc.) when the 'CONSUMER_REGISTRATION'
flag is set to True
.
Running migrations every single time the value of 'CONSUMER_REGISTRATION'
is changed would make much more mess than leaving ConsumerRegistration
table empty.
As indicated by @dahrens, you could isolate the ConsumerRegistration
model along with relevant logic in a separate app, which would only be installed as needed by developers.
3đź‘Ť
The accepted answer doesn’t really provide a way to do what the OP is asking, which is to conditionally declare a model.
People may have various reasons for wanting to do this, from not declaring a model at all, to declaring models differently based on settings (it is implied that if you are doing this: you are intend to run the same code base in different places using different settings).
One solution is to put the model in its own app, and conditionally include the app based on a setting:
# Set this your per-project settings:
CONSUMER_REGISTRATION = True
# Set this in the generic settings
INSTALLED_APPS = [...]
if CONSUMER_REGISTRATION:
INSTALLED_APPS.append('consumer_registration') # Models will be loaded.
There’s nothing wrong with creating an app which just contains a model.
With regards to “automating” it, the table will be created if migrations are run when the setting is true. It will not delete the table if it is changed to false.