How to create a new model at the moment when form is saved

I have a form in my web and I want to be able to create a model upon saving.

For example: In my form I key in Testing. This ‘Testing’ is stored a existing table in the database upon clicking the save button and it is registered as id:1. I would like to create another table at the same time upon saving which is called NextTest_(id) with its own fields which is the case the table will be called ‘NextTest_1’.

This is not something that the Django ORM supports. It’s expecting all models and their underlying tables to exist at the time the system is started.

If you wanted to completely manage the creation and access to those tables directly through raw SQL, you could write something to implement that, but that’s going to need to effectively bypass Django.

Also see Trouble creating a module and the threads referenced by it for some additional information.

Thanks for the clarification ! U said about writing something to implement this. May I ask is there some documentation to do such a thing?

No, because this is something that would be completely outside of Django.

Personally, I’m not familiar with anyone having done this. I mentioned that idea as a side note to acknowledge that there is a way of doing it, but it’s really an extreme case. From what I’ve seen, projects implemented in Django that have thought that they need to dynamically create tables end up being redesigned to not need to do that.

Those projects I’ve known to dynamically create tables have not been built with Django. It’s just not something that Django is designed to do.

Oh okay. Thank you very much !

Sorry about this but i got another question. Those projects u encounter, what did they do to replace this task? Or do u have some suggestions? I was told multiple tables are still needed for this web.

There are, generally speaking, three different sub-cases for this.

The first and easiest case is when all the new tables-to-be-created have the same columns (fields). In that case, you create two tables. The first table has one row per “table”, to give it a name and to serve as the target of a foreign key. The second table has all the necessary columns, and a foreign key to the first table, to ensure that every row inserted can be identified within it’s “logical” table.

Case 2 is where there are more than one structure / schema of these new tables. Very close to the first case except you’re creating all the tables up front, and adding an additional field to the first table to identify which model applies to that logical table. (If the number of such tables is potentially large - > 100 or so, then I’d look at redesigning / refactoring those schemas. There’s a chance that an identified needs for that many predefined dynamic tables indicates a poor data structure.

Case 3 is for those cases where the schema is not known in advance. In that case you’ve got three distinct options

  1. Create your data table as an EAV structure.
  2. Store the data as a JSON / XML / “Document structure”
  3. Use a different framework that supports something like this. (For example SQLAlchemy.)

(The last time we used SQLAlchemy in order to do this was in 2014, before JSONB was known to be a viable alternative.)

Thanks for a detail answer !