如何使用属性非常不同的两种类型的用户设置 Django 模型 [英] How to set up Django models with two types of users with very different attributes
问题描述
注意:我已经再次提出这个问题 考虑到自 1.5 版以来 Django 用户模型的更新.
Note: I've since asked this question again given the updates to Django's user model since version 1.5.
我正在重建和改进现有的 Django 站点,并将其从 Webfaction 转移到 Heroku,并从 Amazon 的 SimpleDB 转移到 Heroku Postgres(尽管在开发时在 Sqllite3 上进行本地测试).我正在做的很多事情是转而使用内置的 Django 功能,例如 Django 管理员、用户身份验证等.
I'm rebuilding and making improvements to an already existing Django site and moving it over from Webfaction to Heroku, and from Amazon's SimpleDB to Heroku Postgres (though testing locally on Sqllite3 when developing). A lot of what I'm doing is moving over to use built-in Django functionality, like the Django admin, user authentication, etc.
从概念上讲,该网站有两种用户:学生和企业.这两种类型的用户拥有完全不同的权限和存储的关于他们的信息.原来如此,在网站原有的结构中,我们建立了如下数据模型:
Conceptually, the site has two kinds of users: Students and Businesses. The two types of users have completely different permissions and information stored about them. This is so much the case that in the original structure of the site, we set up the data model as follows:
Users
ID (primary_key)
Business_or_Student ('B' if business, 'S' if student)
email (unique)
password (hashed, obviously)
...
Students
ID (Foreignkey on Users)
<more information>
...
Businesses
ID (Foreignkey on Users)
<more information>
...
这对我们来说效果很好,我们在 Users 表中有基本的用户信息,然后在 Students 和 Businesses 表中有任何更详细的信息.获取用户的完整个人资料需要使用此伪代码中的某些内容:
This worked pretty well for us, and we had the bare-bones user information in the Users table, and then any more detailed information in the Students and Businesses tables. Getting a user's full profile required something along this pseudocode:
def get_user_profile(id):
if Users(id=id).Business_or_Student = 'B':
return Businesses(id=id)
else:
return Students(id=id)
在移动过程中,我发现 Django 的内置 User
对象的功能非常有限,我不得不使用 UserProfile
类来扩展它我已经创建,然后有额外的 Student
和 Business
表.考虑到我在 Django admin 中进行的所有修补工作,并且我对 Django 模型相对不熟悉,因为我总是以不同的方式做,我不确定这是否是最好的方法,或者我是否应该只需在 UserProfile
表中粘贴所有企业和学生的信息,并用不同的组区分这两者,或者如果有某种方法可以在内置的-in User
对象.
In moving over, I've found that Django's built-in User
object has pretty limited functionality, and I've had to extend it with a UserProfile
class I've created, and then had additional Student
and Business
tables. Given all of the patching I'm doing with this in the Django admin, and being relatively unfamiliar with Django models since I always did it differently, I'm not sure if this is the best way to go about it, or if I should just stick all of the information for businesses and students in the UserProfile
table and just differentiate the two with different groups, or if there's even some way to do this all in the built-in User
object.
由于企业和学生也有不同的界面,我正在认真考虑在我的 Django 项目中将两者设置为不同的应用程序,从而完全分离它们的视图、模型等.这看起来像:
Since businesses and students also have different interfaces, I'm seriously considering setting up the two as different apps within my Django project, and so separating their views, models, etc. entirely. That would look something like:
MyProject/
MyProject/ (project folder, Django 1.4)
mainsite/
students/
businesses/
我最关心的问题之一是 Django 管理员.在扩展 User
时,我必须添加以下代码:
One of my biggest concerns is with the Django Admin. In extending User
, I already had to add the following code:
class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'profile'
class UserAdmin(UserAdmin):
inlines = (UserProfileInline, )
但是,当 User
被拉起时,我希望用户的商业或学生方面的信息显示在 Django 管理中,但 ForeignKey
模型的一部分在 Student
和 Business
模型中,因为每个 Student
/Business
都有一个 User
但每个 User
只有一个 Student
或 一个 Business
对象与之相连.我不确定如何为管理员添加条件内联.
However, I would like the information for the Business or Student aspects of the user to show up in the Django admin when that User
is pulled up, but the ForeignKey
part of the model is in the Student
and Business
model since every Student
/Business
has a User
but every User
has only one Student
or one Business
object connected with it. I'm not sure how to add a conditional Inline for the Admin.
问题:鉴于这种结构和这些顾虑,设置此站点的最佳方式是什么,尤其是数据模型?
推荐答案
这不是一个完整的解决方案,但它会让您知道从哪里开始.
This is not a complete solution, but it will give you an idea of where to start.
- 在
mainsite
中创建一个UserProfile
模型.这将保存两种类型用户的任何共同属性.使用OneToOne(...)
字段将其与User
模型相关联. - 在每个应用程序中再创建两个模型(学生/企业),
Business
和Student
,它们具有OneToOne
关系,每个模型都与UserProfile
(或从UserProfile
继承).这将保存特定于该类型用户的属性.文档:多表继承/一对一关系 - 您可以在
UserProfile
中添加一个字段来区分它是企业还是学生的个人资料.
- Create a
UserProfile
model inmainsite
. This will hold any common attributes for both types of users. Relate it to theUser
model with aOneToOne(...)
field. - Create two more models in each app, (student/business),
Business
andStudent
, which haveOneToOne
relationships each withUserProfile
(or inherit fromUserProfile
). This will hold attributes specific to that type of users. Docs: Multitable inheritance / OneToOne Relationships - You may add a field in
UserProfile
to distinguish whether it is a business or student's profile.
然后,对于内容管理:
- 定义
save()
函数以自动检查冲突(例如,Business
和Student
都有一个条目与UserProfile
,或没有条目). - 在必要时定义
__unicode__()
表示.
- Define the
save()
functions to automatically check for conflicts (e.g. There is an entry for bothBusiness
andStudent
associated with aUserProfile
, or no entries). - Define the
__unicode__()
representations where necessary.
这篇关于如何使用属性非常不同的两种类型的用户设置 Django 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!