【问题标题】:How to extend the django-comments model如何扩展 django-comments 模型
【发布时间】:2014-12-19 17:09:23
【问题描述】:

(对不起我的英语不好,我是一个破旧的法国人)

我尝试扩展 django 评论框架以添加喜欢/不喜欢系统。 阅读 the documentation 后,我已将此添加到我的 model.py 中:

from django.contrib.comments.models import Comment

class Commentslikes(Comment):
    positif = models.IntegerField(default=0)
    negatif = models.IntegerField(default=0)

启动命令python manage.py syncdb 后,django 创建了 cmetslikes mysql 表,其中包含 3 个列:comment_ptr_idpositif否定。没关系。

在我的 view.py 文件中,我已经用这个覆盖了评论帖子视图:

   def custom_comment_post(request, next=None, using=None):

        #Post the comment and get the response
        response = contrib_comments.post_comment(request, next, using)

        if type(response) == HttpResponseRedirect:
           redirect_path, comment_id = response.get('Location').split( '?c=' )
           if comment_id:
                comment = Comment.objects.get( id=comment_id )
                if comment:

               #For test, i try to add 20 positif likes, 10 dislikes and edit the comment with 'foo'
                comment.positif = 20
                comment.negatif = 10
                comment.comment = 'foo'
                comment.save()
                return HttpResponseRedirect( redirect_path + "#c" + comment_id)
       return response

然后我发布了一条测试评论。评论已用 'foo' 修改,但在 cmetslikes 表中没有添加任何行,其 id 为评论,positif 为 20,negatif 为 10。 中未添加评论的行cmets喜欢

我忘记或做了什么?

谢谢, 托马斯

【问题讨论】:

  • 您好!只是为了让你知道,上周我和 Django 的核心贡献者之一在一起,他提到最好不要使用 Django 的 cmets,因为它们可能会被弃用。

标签: python django extend


【解决方案1】:

你好托马斯,

好吧,您已经使用 Commentslikes 扩展了 Comment,因此您可能希望在视图中使用该类“Commentslikes”;不是“评论”。

旁注:

  • 通常最好避免在 Model 类名中使用复数,最好使用驼峰式。
  • 您可以改用合成。

【讨论】:

    猜你喜欢
    • 2011-01-11
    • 1970-01-01
    • 2020-11-29
    • 2020-05-20
    • 1970-01-01
    • 2013-12-23
    • 2016-10-12
    • 2011-02-28
    • 2013-04-10
    相关资源
    最近更新 更多