Hello, I’d like to enable the feature of dynamically changing models.py in my project. However, when I thought everything was fine, I encountered the following error message:
“Cannot query “table_model_001 object (1)”: Must be “table_model_001” instance.”
Here are the steps I followed:
Used a function in views.py to modify models.py.
Attempted to update the model stored in the application simultaneously, using “type(model_name, (instance_model,), attrs)”.
Ran makemigrations: “call_command(‘makemigrations’, ‘app_name’)”.
Ran migrate: “call_command(‘migrate’, app_label=‘app_name’)”.
Tried to delete data and received the error message: “omdata_model.objects.filter(data_no=data_no).delete()”.
I would like to know if Django supports such functionality or if there is an error in my steps. Thank you.
I sometimes do this when I just want a nice class to capture my raw sql for reporting… One day I might propose that we allow a “lightweight model for read-only raw sql” formally because it will break strict tests that error on warning and this approach will raise duplicate registration warnings.
But as Carlton mentioned… yeah you want static models if you need any kind of schema syncing to the db.