且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用属性非常不同的两种类型的用户设置 Django 模型

更新时间:2023-11-13 21:58:10

这不是一个完整的解决方案,但它会让您知道从哪里开始.

This is not a complete solution, but it will give you an idea of where to start.

  • mainsite 中创建一个UserProfile 模型.这将保存两种类型用户的任何共同属性.使用 OneToOne(...) 字段将其与 User 模型相关联.
  • 在每个应用程序中再创建两个模型(学生/企业),BusinessStudent,它们具有 OneToOne 关系,每个模型都与 UserProfile(或从 UserProfile 继承).这将保存特定于该类型用户的属性.文档:多表继承/一对一关系
  • 可以UserProfile中添加一个字段来区分它是企业还是学生的个人资料.
  • Create a 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.
  • Create two more models in each app, (student/business), 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 Relationships
  • You may add a field in UserProfile to distinguish whether it is a business or student's profile.

然后,对于内容管理:

  • 定义 save() 函数以自动检查冲突(例如,BusinessStudent 都有一个条目与 UserProfile,或没有条目).
  • 在必要时定义 __unicode__() 表示.
  • Define the 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).
  • Define the __unicode__() representations where necessary.