如何在 django 中使用自己的函数扩展 request.user ?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看代理模型: http://docs.djangoproject.com/ en/dev/topics/db/models/#id8
Check out proxy models: http://docs.djangoproject.com/en/dev/topics/db/models/#id8
我认为您看到的代码示例似乎是从其他地方挑选的(它不是 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 :).