Django——模型构建(用户配置文件或单独模型)
标题的措辞可能有点令人困惑。我有一个应用程序,其中的用户具有不同的权限级别(管理员和非管理员)。
我不确定以下哪种结构更可取:只需使用 User_Profile 并添加一个管理字段或创建两个模型(一个用于管理员,一个用于)
class User_Profile(models.Model):
user=models.ForeignKey(User, unique=True)
employer=models.ForeignKey(Company,blank=True)
admin=models.BooleanField(default=False)
phone=models.CharField(blank=True)
这是否比为管理员创建一个单独的表(仅通过链接到用户和雇主)更可取外键?我的视图将检查每个用户的管理员状态,因此我担心一遍又一遍地查询大型 User_Profile 表可能会变得昂贵。
可能还没有到优化这一点的阶段,但我只是对最佳实践感到好奇。
The phrasing of the title may be a little confusing. I have an app that has users with varying permission levels (admin and non-admin).
I was unsure which of the following constructs was preferable: just go with User_Profile and add an admin field or create two models (one for admin and one for )
class User_Profile(models.Model):
user=models.ForeignKey(User, unique=True)
employer=models.ForeignKey(Company,blank=True)
admin=models.BooleanField(default=False)
phone=models.CharField(blank=True)
Is this preferable to creating a separate table for Admins that just links to User and Employer through ForeignKeys? My views will be checking each user for Admin status, so I was concerned that querying a large User_Profile table over and over again might become expensive.
Probably not at the stage to optimize this, but I'm just curious about best practice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实际上推荐 Django 的内置用户权限而不是这些选项:
https://docs.djangoproject.com/en/dev/topics/auth/ #methods
这是一个帮助您入门的快速教程:
http://parand.com/说/index.php/2010/02/19/django-using-the-permission-system/
I'd actually recommend Django's built in user permissions over either of those options:
https://docs.djangoproject.com/en/dev/topics/auth/#methods
Here's a quick tutorial to help you get started:
http://parand.com/say/index.php/2010/02/19/django-using-the-permission-system/