简体   繁体   English

扩展Django用户模型(使其在request.user中工作),Django 1.2.3

[英]Extend Django User model (make it work in request.user), Django 1.2.3

I'd like to extend the User model, so I can put in some extra fields and functions (not only fields, keep in mind, or else (well, still) I could use get_profile(), which I think is ugly). 我想扩展User模型,所以我可以添加一些额外的字段和函数(不仅要记住字段,否则,(仍然可以)我可以使用get_profile(),我认为这很丑陋)。

Also I would like to use that new extended User model in request.user, like this: 我也想在request.user中使用新的扩展用户模型,如下所示:

Extended User model: 扩展用户模型:

# imports etc

class CustomUser(User):
    extra_field = ...

    def extra_function(self):
        ...

        return ...

Example usage view: 用法视图示例:

# imports etc

def extra_function_view(request):
    print request.user.username
    print request.user.extra_field

    request.user.extra_function()

    return HttpResponse( ... )

The above code obviously doesn't work, because extra_field and extra_function are not in the User model. 上面的代码显然不起作用,因为extra_field和extra_function不在User模型中。

Now I found a method to accomplish this, using an authentication backend, which is kinda complex, and which I couldn't get to work in Django 1.2.3. 现在我找到了一种方法来实现这一点,使用身份验证后端,这有点复杂,我无法在Django 1.2.3中工作。

AUTHENTICATION_BACKENDS = (
    'myproject.auth_backends.CustomUserModelBackend',
)
...

CUSTOM_USER_MODEL = 'accounts.CustomUser'

Further more tried some other methods, like using signals, which didn't work. 还有更多的人尝试了其他一些方法,例如使用信号,但这些方法无效。 To me, the only solution seems to adjust the User model within Django (which isn't an elegant solution, adjusting Django's source code, but is code-wise a neat solution). 对我来说,唯一的解决方案似乎是调整Django中的User模型(这不是一个优雅的解决方案,调整Django的源代码,但在代码方面是一个简洁的解决方案)。

So I'm looking for a solution for this.. has anyone done this before? 所以我正在寻找解决方案..有人做过吗?

Thanks for now 现在谢谢

Stefan 斯特凡

You're on the right track with adjusting the Authentication Backend. 您正在调整身份验证后端的正确轨道。

Authentication backends can be pretty simple, here is a snip of ours we use in our project. 身份验证后端可以非常简单,这是我们在项目中使用的剪辑。

class LocalAccount(object):
    def authenticate(self, system=None, username=None, password=None):
        try:
            user = User.objects.get(system=system, alias=username)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        'returns user'

    def get_all_permissions(self, user_obj):
        'returns user permissions'

    def has_perm(self, user_obj, perm):
        'verifies permission, remember you can hardcode these'

    def get_group_permissions(self, user_obj):
        return set() #We don't use this

    def has_module_perms(self, user_obj, app_label):
        return False

This allows you to return whatever user model you wish to return. 这允许您返回要返回的任何用户模型。 As long as your user model is basically like Django's user model you won't run into any issues. 只要您的用户模型基本上像Django的用户模型,您就不会遇到任何问题。

Also keep in mind that extending user and including the application will cause both the Django User model and your own User model to be in the database with model inheritance. 还要记住,扩展用户和包括应用程序将导致Django用户模型和您自己的用户模型都在模型继承的数据库中。 Your best option is to copy the Django User model and alter it. 您最好的选择是复制Django用户模型并进行更改。 For our project we don't even include 'auth' in the installed apps setting. 对于我们的项目,我们甚至不在已安装的应用程序设置中包含“auth”。

The concept here is Python duck typing . 这里的概念是Python 鸭子输入 As long as the User model that you create has the same interface that Django is looking for (methods, fields) it doesn't matter if it's actually the User class it specified. 只要您创建的User模型具有与Django正在寻找的相同的接口(方法,字段),实际上是否是它指定的User类就没有关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM