如何在 Django 中使用 Post_save

发布于 2024-10-02 23:56:00 字数 353 浏览 1 评论 0原文

我试图在用户提交评论后使用 Django 评论框架向他们的个人资料添加积分。我想我需要使用 post_save 但我不确定是否完全诚实。

这是我在 models.py 中的方法:

 def add_points(request, Comment):
    if Comment.post_save():
        request.user.get_profile().points += 2
        request.user.get_profile().save()

从我发现的 post_save 示例中,这与显示的内容相去甚远 - 所以我认为我偏离了目标。

感谢您的帮助。

I am trying to add points to a User's profile after they submit a comment- using the Django comment framework. I think I need to use a post_save but am not sure to be perfectly honest.

Here is what I have as a method in my models.py:

 def add_points(request, Comment):
    if Comment.post_save():
        request.user.get_profile().points += 2
        request.user.get_profile().save()

From the examples of post_save I've found, this is far from what is shown - so I think I am way off the mark.

Thank you for your help.

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

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

发布评论

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

评论(1

我要还你自由 2024-10-09 23:56:00

不幸的是,这根本没有意义。

首先,这不能是一个方法,因为它没有 self 作为第一个参数。

其次,似乎是在上课,而不是在实例。您无法保存类本身,只能保存它的实例。

第三,post_save 不是模型的一种方法(除非您自己定义了一个方法)。它是一个信号,您不调用信号,而是将信号处理程序附加到它并在那里执行逻辑。您也无法将数据从信号返回到方法。

最后,您添加 2 的配置文件实例不一定与您在第二行中保存的配置文件实例相同,因为 Django 模型实例没有标识。获取一次并将其放入变量中,然后保存。

评论框架定义了自己的信号,您可以使用它们通用 post_save 的。因此,您实际需要的是在 comment_was_posted 上注册一个信号处理程序。在该处理程序中,您需要获取用户的个人资料并更新它。

def comment_handler(sender, comment, request, **kwargs):
    profile = request.user.get_profile()
    profile.points += 2
    profile.save()

from django.contrib.comments.signals import comment_was_posted
comment_was_posted.connect(comment_handler, sender=Comment)

Unfortunately this makes no sense at all.

Firstly, this can't be a method, as it doesn't have self as the first parameter.

Secondly, it seems to be taking the class, not an instance. You can't save the class itself, only an instance of it.

Thirdly, post_save is not a method of the model (unless you've defined one yourself). It's a signal, and you don't call a signal, you attach a signal handler to it and do logic there. You can't return data from a signal to a method, either.

And finally, the profile instance that you add 2 to will not necessarily be the same as the one you save in the second line, because Django model instances don't have identity. Get it once and put it into a variable, then save that.

The Comments framework defines its own signals that you can use instead of the generic post_save. So, what you actually need is to register a signal handler on comment_was_posted. Inside that handler, you'll need to get the user's profile, and update that.

def comment_handler(sender, comment, request, **kwargs):
    profile = request.user.get_profile()
    profile.points += 2
    profile.save()

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