gpt4 book ai didi

django - 在 Django 中为不同类型的用户建模帐户

转载 作者:行者123 更新时间:2023-12-04 19:20:06 24 4
gpt4 key购买 nike

假设您有一个应用程序,不同类型的用户可以在其中签名:公司、律师和客户。一个事务所有很多律师;一个律师有很多客户。事务所用户的观点当然不同于律师用户的观点;两者与客户端用户不同。

您将如何为三个不同的用户建模?我可以考虑以下方法:

三种不同型号的 ForeignKeyUser ,每个都有自己的领域,例如:

class Firm(models.Model):
user = models.ForeignKey(User)
class Lawyer(models.Model):
user = models.ForeignKey(User)
specialty = models.CharField(max_length=100)
class Client(models.Model)
user = modelsForeignKey(User)

例如,现在您可以使用两个 ForeignKeys 将咨询创建为单独的模型。 : 到 Lawyer并到 Client ;您还可以通过创建模型向咨询添加资源(如文档或类似内容) ResourceForeignKeyConsultation .

这种方式很难区分用户:你怎么知道一个 userFirm ,例如 - 您需要多次查询数据库或分配 Profile到通用 User目的。

您也可以只添加 ProfileUser并包括一个 Role ,然后根据 user.get_profile().role 引导 View 和身份验证.

你会如何处理这个问题?

最佳答案

我会做你在这里建议的:

You could also add only a Profile to the User and include a Role, and then you channel the views and authentication based on user.get_profile().role.



创建带有角色选择字段的配置文件。创建一些装饰器,例如 @lawyer_only,确保您的 View 只能由 Lawyer 角色用户访问。

关于django - 在 Django 中为不同类型的用户建模帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4565537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com