# I'd like to enable the feature of dynamically changing \`models.py\` in my project.

**URL:** <https://forum.djangoproject.com/t/id-like-to-enable-the-feature-of-dynamically-changing-models-py-in-my-project/25599>\
**Category:** ORM\
**Created:** [November 23, 2023, 11:06am UTC](https://forum.djangoproject.com/t/id-like-to-enable-the-feature-of-dynamically-changing-models-py-in-my-project/25599 "2023-11-23T11:06:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gusto0115](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gusto0115/32/17286_2.png) [@gusto0115](https://forum.djangoproject.com/u/gusto0115)\
**Post date:** [November 23, 2023, 11:06am UTC](https://forum.djangoproject.com/t/id-like-to-enable-the-feature-of-dynamically-changing-models-py-in-my-project/25599/1 "2023-11-23T11:06:05Z")

</div>

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:

1. Used a function in `views.py` to modify `models.py`.
2. Attempted to update the model stored in the application simultaneously, using “type(model\_name, (instance\_model,), attrs)”.
3. Ran `makemigrations`: “call\_command(‘makemigrations’, ‘app\_name’)”.
4. Ran `migrate`: “call\_command(‘migrate’, app\_label=‘app\_name’)”.
5. 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.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [November 23, 2023, 11:11am UTC](https://forum.djangoproject.com/t/id-like-to-enable-the-feature-of-dynamically-changing-models-py-in-my-project/25599/2 "2023-11-23T11:11:01Z")

</div>

This isn’t something Django supports, not really.

Models are (just) Python classes, so you can in theory construct them at runtime, but syncing to the DB, and … — No.

There’s a page on the [Django Wiki about it from the 0.96 days](https://code.djangoproject.com/wiki/DynamicModels) but I can’t say what it would take to get it going in a modern Django.

Searching for that wiki page turned up this [Reddit comment](https://www.reddit.com/r/django/comments/wsi256/is_it_possible_to_create_models_dynamically/):

> Is it possible? yes. is it easy? no. is it wise? very much no.

That seems about right to me.

---

<div class="post-metadata">

**Author:** ![shangxiao](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shangxiao/32/10313_2.png) [@shangxiao](https://forum.djangoproject.com/u/shangxiao)\
**Post date:** [November 23, 2023, 11:25am UTC](https://forum.djangoproject.com/t/id-like-to-enable-the-feature-of-dynamically-changing-models-py-in-my-project/25599/3 "2023-11-23T11:25:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [November 23, 2023, 11:30am UTC](https://forum.djangoproject.com/t/id-like-to-enable-the-feature-of-dynamically-changing-models-py-in-my-project/25599/4 "2023-11-23T11:30:47Z")

</div>

@shangxiao Can I ask when you’d do that rather than say use a view (with a `managed=False` model)?

---

<div class="post-metadata">

**Author:** ![stribny](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/stribny/32/5592_2.png) [@stribny](https://forum.djangoproject.com/u/stribny)\
**Post date:** [November 24, 2023, 11:41am UTC](https://forum.djangoproject.com/t/id-like-to-enable-the-feature-of-dynamically-changing-models-py-in-my-project/25599/5 "2023-11-24T11:41:55Z")

</div>

It is possible to implement dynamic (on the fly) models with Django but it is not trivial. We do this in Baserow. If you really need it you can look at [How Baserow lets users generate Django models on the fly // Baserow](https://baserow.io/blog/how-baserow-lets-users-generate-django-models) and Baserow backend codebase.

That being said, most likely you don’t actually need it and should think harder about your schema.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [November 24, 2023, 1:17pm UTC](https://forum.djangoproject.com/t/id-like-to-enable-the-feature-of-dynamically-changing-models-py-in-my-project/25599/6 "2023-11-24T13:17:05Z")

</div>

@stribny I’d forgotten that post — thanks for the reminder!
