Django中的动态数据库表 [英] Dynamic database tables in django

查看:440
本文介绍了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.

在研究的过程中,我发现了这个 Django动态模型字段.我不确定如何使用 django-mutant 来完成此操作.另外,有什么方法可以在不使用任何外部应用程序的情况下做到这一点?

While researching I found this Django dynamic model fields. I am not sure how to use django-mutant to accomplish this. Also, is there any way I could do this without using any external apps?

PS:我正在使用的后端是Mysql

PS : The backend that I am using is 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).

采用建议的解决方案要求您构造包含相关用户表名的SQL语句.对数据库的连续查询在大多数情况下将有所不同,这将减慢工作速度,因为必须准备"每个SQL语句(必须检查语法,必须验证表和列的名称,请求用户的权限访问命名资源必须经过授权,依此类推.)

Adopting your proposed solution requires that you construct SQL statements containing the relevant user's table name. 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).

通过使用单个表(模型),可以重复使用相同的查询,同时使用参数来更改特定的数据值(在这种情况下,是要查找其数据的用户的姓名).您的数据库工作将以更快的速度进行,您只需要一个模型来描述所有用户的数据,数据库管理就不会成为噩梦.

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.

另一个优点是Django(您似乎正在使用)具有广泛的基于用户的权限模型,并且可以轻松地用于对用户登录进行身份验证(一旦您知道如何使用).这些优势非常引人注目,我希望您能摆脱异端,并决定摆脱一个表(如果打算使用标准的Django登录名,那么与用户模型的关系将成为所有Django的核心部分)项目).

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). 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).

在继续操作时,请随时提出更多问题.看来您是数据库工作的新手,所以我尝试提供适当级别的细节.如果您无法获得知识渊博的建议,就会有很多类似的陷阱.这样的人会帮助您.

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.

这篇关于Django中的动态数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆