问题描述
注意:自从 / 然后,对于内容管理: Note: I've since asked this question again given the updates to Django's user model since version 1.5. 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: 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: In moving over, I've found that Django's built-in 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: One of my biggest concerns is with the Django Admin. In extending However, I would like the information for the Business or Student aspects of the user to show up in the Django admin when that Question: Given this structure and these concerns, what is the best way to set up this site, particularly the data model? This is not a complete solution, but it will give you an idea of where to start. Then, for content management: 这篇关于如何使用具有非常不同属性的两种类型的用户来设置Django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
UserProfile
中添加一个字段来区分是商业还是学生的个人资料。
save()
函数自动检查冲突(例如,有 Business
和学生
之间的条目$>
__ unicode __()
必要时的陈述。
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>
...
def get_user_profile(id):
if Users(id=id).Business_or_Student = 'B':
return Businesses(id=id)
else:
return Students(id=id)
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.MyProject/
MyProject/ (project folder, Django 1.4)
mainsite/
students/
businesses/
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
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.UserProfile
model in mainsite
. This will hold any common attributes for both types of users. Relate it to the User
model with a OneToOne(...)
field.Business
and Student
, which have OneToOne
relationships each with UserProfile
(or inherit from UserProfile
). This will hold attributes specific to that type of users. Docs: Multitable inheritance / OneToOne RelationshipsUserProfile
to distinguish whether it is a business or student's profile.save()
functions to automatically check for conflicts (e.g. There is an entry for both Business
and Student
associated with a UserProfile
, or no entries).__unicode__()
representations where necessary.