如何在 django 中使用自己的函数扩展 request.user ?

发布于 2024-08-23 07:19:28 字数 700 浏览 4 评论 0原文

我在 django- ratings 文档上看到了一些漂亮的代码,并且喜欢创建类似的东西。在谷歌搜索了两周后,我不知道如何做到这一点。

也许您可以帮助我搜索什么或从哪里获取一些文档?

来自 django- ratings 文档的代码:

...
response = AddRatingView()(request, **params)
    if response.status_code == 200:
        if response.content == 'Vote recorded.':
            request.user.add_xp(settings.XP_BONUSES['submit-rating'])
        return {'message': response.content, 'score': params['score']}
    return {'error': 9, 'message': response.content}
...

我的问题:

request.user.add_xp(settings.XP_BONUSES['submit-rating'])

所以我想做这样的事情:

request.user.my_shiny_function(foobar)

提前致谢, 托马斯

I've seen some nifty code on django-ratings documentation and like to create something similar. After googling around for now 2 weeks I got no idea on how to do this.

Maybe you could help me what to search for or where to get some docs?

Code from django-ratings docs:

...
response = AddRatingView()(request, **params)
    if response.status_code == 200:
        if response.content == 'Vote recorded.':
            request.user.add_xp(settings.XP_BONUSES['submit-rating'])
        return {'message': response.content, 'score': params['score']}
    return {'error': 9, 'message': response.content}
...

My Problem:

request.user.add_xp(settings.XP_BONUSES['submit-rating'])

So i'd like to do something like this:

request.user.my_shiny_function(foobar)

Thanks in advance,
Thomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对风讲故事 2024-08-30 07:19:28

我认为您看到的代码示例似乎是从其他地方挑选的(它不是 django- ratings 代码的一部分 - 源目录上的简单 grep -ir "add_xp" 显示该文本仅在 Readme.rst 中)。

如果您能解释为什么需要您正在寻找的功能,也许我们可以提供更多帮助。同时,您可以考虑滚动自己的 自定义后端,扩展默认的User模型,然后向其中添加其他“漂亮”功能:)。

I think the code sample you're sighting seems to have been picked from somewhere else (it's not part of the django-ratings code - a simple grep -ir "add_xp" on the source directory shows that text is only in Readme.rst).

If you could explain why you need the functionality you're looking for here, maybe we could help some more. In the mean time, you can look at rolling your own custom backend, extending the default User model and then adding other "nifty" features to it :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文