I
I
ivanesi2014-05-23 18:39:30
MySQL
ivanesi, 2014-05-23 18:39:30

How to dynamically create tables in a database in Django based on a condition?

Good afternoon.
I'm quite new to development and Django. I understand that you need to read tutorials, take lessons before implementing something of your own, but I can’t resist - there is a project that I really want to do.
How to create tables in the database depending on user actions? For example, he added some material, the description of which I specified in models.py, and I need to create tables that were not previously in the database along with this material, associated with tables of the material described in models.py for which Django has already created tables.
The condition for the appearance of tables in the database is the appearance of data in existing tables of the database.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Kulakov, 2015-12-08
@carbon88

I took a quick look at your diagram.
1) there is one rule when building ER models, it sounds something like this: "dead crows fly east". its meaning is that the tables are located on the diagram so that directories and rarely changed tables accumulate in the lower right corner, and, accordingly, the most changed tables in the upper left. If you follow this rule, the readability of the database diagram improves significantly. Example: 468066d665e44b43a69db7971676bcbc.PNG
2)User. according to the rules, the model is adjusted to 3.5NF (or Boyce-Codd Normal Form). ( List of normal forms ). And so the table User is not normalized properly.
Usually contacts are taken out in a separate table (at least I would do that). Duplicate records are also possible for addresses.
If you have an indication of one of the metro stations is mandatory for the user, then a link to the city is not needed if there is a link to the metro. it is obvious that each subway belongs to one city. If the metro is not required, then we leave a link to the city.
3) product. Usually a product is some nomenclature, that is, a description of the properties of the product, its name and other things. The price is not included. What will you do if products come from different batches and their purchase price is different? Your model does not reflect this. usually there is a price list in which there is a correspondence between the product and the price. price lists in real life can change and have a date on which this price list is valid. Then the situation I have indicated will not happen. The price list is drawn up with at least two tables - the price list itself with general parameters such as the date of creation, compiler ect. and a tabular part, where the positions of all price lists are recorded (with a link, of course) indicating the product and its cost for a specific price list. The currency can be specified both in specific positions of the price list and globally on the price list.
"For all pages of pages, the url table is a parent, I would like that when deleting, for example, a product, the url is deleted, now vice versa."
Do not quite understand. who has everyone? I think mysql has settings for foreign keys OnUpdate, OnDelete. it is possible through them to organize the correct removal.
"The table of users is one for legal entities and individuals, it is not possible to correctly divide."
Normally can be divided. Later today I will write how. The work day is over and you need to leave the computer.

M
maxaon, 2014-05-23
@ivanesi

You don't seem to be doing that. Dynamic creation of models and tables is a very specific task. If you create a new table every time a user clicks, that's wrong.
But for reference - creating dynamic models

P
Pavel Solovyov, 2014-05-23
@pavel_salauyou

maybe you should use document-oriented databases

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question