• <bdo id='0ZwTk'></bdo><ul id='0ZwTk'></ul>

    1. <i id='0ZwTk'><tr id='0ZwTk'><dt id='0ZwTk'><q id='0ZwTk'><span id='0ZwTk'><b id='0ZwTk'><form id='0ZwTk'><ins id='0ZwTk'></ins><ul id='0ZwTk'></ul><sub id='0ZwTk'></sub></form><legend id='0ZwTk'></legend><bdo id='0ZwTk'><pre id='0ZwTk'><center id='0ZwTk'></center></pre></bdo></b><th id='0ZwTk'></th></span></q></dt></tr></i><div id='0ZwTk'><tfoot id='0ZwTk'></tfoot><dl id='0ZwTk'><fieldset id='0ZwTk'></fieldset></dl></div>
    2. <small id='0ZwTk'></small><noframes id='0ZwTk'>

        <legend id='0ZwTk'><style id='0ZwTk'><dir id='0ZwTk'><q id='0ZwTk'></q></dir></style></legend>
        <tfoot id='0ZwTk'></tfoot>

        在 Django 1.5 中使用多个 AUTH_USER_MODEL 的正确方法是什么?

        What#39;s the proper way to use multiple AUTH_USER_MODEL in Django 1.5?(在 Django 1.5 中使用多个 AUTH_USER_MODEL 的正确方法是什么?)
        • <i id='SRgRk'><tr id='SRgRk'><dt id='SRgRk'><q id='SRgRk'><span id='SRgRk'><b id='SRgRk'><form id='SRgRk'><ins id='SRgRk'></ins><ul id='SRgRk'></ul><sub id='SRgRk'></sub></form><legend id='SRgRk'></legend><bdo id='SRgRk'><pre id='SRgRk'><center id='SRgRk'></center></pre></bdo></b><th id='SRgRk'></th></span></q></dt></tr></i><div id='SRgRk'><tfoot id='SRgRk'></tfoot><dl id='SRgRk'><fieldset id='SRgRk'></fieldset></dl></div>

                  <tbody id='SRgRk'></tbody>

                <small id='SRgRk'></small><noframes id='SRgRk'>

                  <bdo id='SRgRk'></bdo><ul id='SRgRk'></ul>

                  <tfoot id='SRgRk'></tfoot>
                  <legend id='SRgRk'><style id='SRgRk'><dir id='SRgRk'><q id='SRgRk'></q></dir></style></legend>
                  本文介绍了在 Django 1.5 中使用多个 AUTH_USER_MODEL 的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想为 django.contrib.auth 模块使用两种不同的模型.第一个是 Django 提供的默认用户模型,它完全适合管理员访问(组、权限等),但另一个是客户模型,它具有许多不同的属性(城市、区域设置、地址等)默认用户模型.这些用户组必须使用不同的表,并且不能有任何关系.

                  I want to use two different models for django.contrib.auth module. The first one is the default User model provided by Django which is completely suitable for admin access (groups, permissions etc.) but the other one is customer model which has a lot of different attributes (city, locale, address etc.) compared to default User model. These user groups must use different tables and mustn't have any relation.

                  我创建了一个继承自 AbstractBaseUser 的 Customer 模型和一个名为 ChangeBaseUser 的中间件类,如下所示:

                  I created a Customer model inherited from AbstractBaseUser and a middleware class called ChangeBaseUser like this:

                  class ChangeBaseUser(object):
                      def process_request(self, request):  
                          match = resolve(request.path)
                          if match.app_name == "myapp":
                              settings.AUTH_USER_MODEL = 'myapp.Customer'
                          else:
                              settings.AUTH_USER_MODEL = 'auth.User'
                  

                  它正在工作,但我不确定这是否是正确的方法,因为在文档中有一个部分(link) 这意味着方便的方法是为默认用户模型分配静态值.

                  It's working but I'm not sure whether this is the proper way to do it because in documentation there is a section (link) that implies the convenient way is to assign a static value for default user model.

                  如果这不是正确的方法,您对每个模块有多个用户模型有什么建议吗?

                  If this is not the proper way, do you have any suggestions for having multiple user models per module basis?

                  推荐答案

                  如果您的要求是将管理员用户和客户分开,我认为拥有多个用户模型没有任何问题.在这一点上,客户模型就像任何模型一样,除了它与用户模型非常相似,如果它适合你,那就太好了.唯一的缺点是您可能必须复制 django 为 Django 用户模型提供的许多帮助程序,例如身份验证后端或用户会话.如果你愿意做这一切,这似乎完全没问题.

                  If your requirement is to keep admin users and customers separate, I don't see anything wrong with having multiple user models. At this point, the customer model is like any model, except it is very similar to the user model and that is perfectly fine if that works for you. The only disadvantage is that you will have to possibly duplicate many helpers django gives you for the Django user model such as auth backend or sessions for users. If you are willing to do all that, this seems perfectly fine.

                  如果您希望使用许多 django 助手,您可能需要创建一个非常基本的用户模型,作为管理员和客户的基础:

                  If you wish however to utilize many of the django helpers you might want to create a very basic user model which will serve as a base for both admins and customers:

                  class User(AbstractBaseUser):
                      # use this for auth and sessions
                  
                  class Admin(models.Model):
                      user = models.OneToOneField(UserBase, related_name='admins')
                      # ... other admin-specific fields
                  
                  class Customer(models.Model):
                      user = models.OneToOneField(UserBase, related_name='admins')
                      # ... other customer-specific fields
                  

                  这将允许您重用 Django 提供的许多开箱即用的东西,但它会产生一些额外的数据库开销,因为必须计算更多的连接.但是你可以为客户缓存一些东西,这样你就可以恢复一些性能.

                  This will allow you to reuse many of the things Django provides out of the box however it will incur some additional db overhead since more joins will have to be calculated. But then you can cache things for customers so you can get some of the performance back.

                  这篇关于在 Django 1.5 中使用多个 AUTH_USER_MODEL 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                • <small id='GZwAg'></small><noframes id='GZwAg'>

                      <tbody id='GZwAg'></tbody>
                      <bdo id='GZwAg'></bdo><ul id='GZwAg'></ul>
                      <legend id='GZwAg'><style id='GZwAg'><dir id='GZwAg'><q id='GZwAg'></q></dir></style></legend><tfoot id='GZwAg'></tfoot>
                          <i id='GZwAg'><tr id='GZwAg'><dt id='GZwAg'><q id='GZwAg'><span id='GZwAg'><b id='GZwAg'><form id='GZwAg'><ins id='GZwAg'></ins><ul id='GZwAg'></ul><sub id='GZwAg'></sub></form><legend id='GZwAg'></legend><bdo id='GZwAg'><pre id='GZwAg'><center id='GZwAg'></center></pre></bdo></b><th id='GZwAg'></th></span></q></dt></tr></i><div id='GZwAg'><tfoot id='GZwAg'></tfoot><dl id='GZwAg'><fieldset id='GZwAg'></fieldset></dl></div>