简体   繁体   English

Django 中的动态数据库表

[英]Dynamic database tables in django

I am working on a project which requires me to create a table of every user who registers on the website using the username of that user.我正在从事一个项目,该项目要求我为使用该用户的用户名在网站上注册的每个用户创建一个表。 The columns in the table are same for every user.表中的列对于每个用户都是相同的。

While researching I found this Django dynamic model fields .在研究时,我发现了这个Django dynamic model fields I am not sure how to use django-mutant to accomplish this.我不确定如何使用django-mutant来实现这一点。 Also, is there any way I could do this without using any external apps?另外,有没有什么办法可以在不使用任何外部应用程序的情况下做到这一点?

PS : The backend that I am using is Mysql PS:我使用的后端是Mysql

An interesting question, which might be of wider interest.一个有趣的问题,可能会引起更广泛的兴趣。

Creating one table per user is a maintenance nightmare.为每个用户创建一个表是一场维护噩梦。 You should instead define a single table to hold all users' data, and then use the database's capabilities to retrieve only those rows pertaining to the user of interest (after checking permissions if necessary, since it is not a good idea to give any user unrestricted access to another user's data without specific permissions having been set).您应该改为定义一个表来保存所有用户的数据,然后使用数据库的功能仅检索与感兴趣的用户有关的那些行(必要时检查权限后,因为给任何用户提供不受限制的权限并不是一个好主意在未设置特定权限的情况下访问其他用户的数据)。

Adopting your proposed solution requires that you construct SQL statements containing the relevant user's table name.采用您建议的解决方案需要您构造包含相关用户表名的 SQL 语句。 Successive queries to the database will mostly be different, and this will slow the work down because every SQL statement has to be “prepared” (the syntax has to be checked, the names of table and columns has to be verified, the requesting user's permission to access the named resources has to be authorized, and so on).对数据库的连续查询大多不同,这会减慢工作速度,因为每个 SQL 语句都必须“准备好”(必须检查语法,必须验证表和列的名称,请求用户的权限访问命名资源必须经过授权,等等)。

By using a single table (model) the same queries can be used repeatedly, with parameters used to vary specific data values (in this case the name of the user whose data is being sought).通过使用单个表(模型),可以重复使用相同的查询,并使用用于改变特定数据值的参数(在这种情况下是正在寻找其数据的用户的姓名)。 Your database work will move along faster, you will only need a single model to describe all users' data, and database management will not be a nightmare.您的数据库工作将进展得更快,您只需要一个模型来描述所有用户的数据,数据库管理将不会是一场噩梦。

A further advantage is that Django (which you appear to be using) has an extensive user-based permission model, and can easily be used to authenticate user login (once you know how).另一个优点是 Django(您似乎正在使用)具有广泛的基于用户的权限模型,并且可以轻松用于验证用户登录(一旦您知道如何使用)。 These advantages are so compelling I hope you will recant from your heresy and decide you can get away with a single table (and, if you planning to use standard Django logins, a relationship with the User model that comes as a central part of any Django project).这些优点是如此引人注目,我希望你能放弃你的异端邪说,并决定你可以摆脱一个单一的表(并且,如果你打算使用标准的 Django 登录,与作为任何 Django 核心部分的 User 模型的关系项目)。

Please feel free to ask more questions as you proceed.请随时提出更多问题。 It seems you are new to database work, and so I have tried to present an appropriate level of detail.您似乎对数据库工作不熟悉,因此我已尝试提供适当级别的详细信息。 There are many pitfalls such as this if you cannot access knowledgable advice.如果您无法获得知识渊博的建议,就会有许多诸如此类的陷阱。 People on SO will help you. SO上的人会帮助你。

This page shows how to create model and install table to database on the fly.此页面显示了如何动态创建模型并将表安装到数据库。 So your could use type('table_with_username', (models.Model,), attrs) to create a model and use django.core.management to install it to database.因此,您可以使用type('table_with_username', (models.Model,), attrs)创建模型并使用django.core.management将其安装到数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM