简体   繁体   English

如何扩展Django评论模型

[英]How to extend the django-comments model

(Sorry for my bad english, i'm a shabby French) (对不起,我的英语不好,我是一个简陋的法国人)

I try to extend the django comment framework to add a like/dislike system. 我尝试扩展django注释框架以添加一个喜欢/不喜欢的系统。 After read the documentation , i have added this to my model.py : 阅读文档后 ,我已将其添加到我的model.py中

from django.contrib.comments.models import Comment

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

After launch the command python manage.py syncdb , django have created the commentslikes mysql table with 3 cols : comment_ptr_id , positif , negatif . 启动命令后python manage.py syncdb ,Django的创建与3周的cols的commentslikes mysql表:comment_ptr_id,positif,negatif。 It's ok. 没关系。

In my view.py file, i have override the comment post view with this : 在我的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

Then I posted a test comment. 然后我发布了测试评论。 Comment has been modified with 'foo' but no rows have been added in the commentslikes table with the id of comment, positif at 20 and negatif at 10. Not row for the comment has added in commentslikes 评论已被修改与“富”,但没有行已在commentslikes表被添加评论的ID,positif在20和negatif在10排不出的评论中commentslikes已加入

I have forgotten or done something ? 我忘记或做了什么?

Thanks, Thomas 谢谢托马斯

Bonjour Thomas, Bonjour Thomas,

well you've extended Comment with Commentslikes so you probably want to use that class "Commentslikes" in your view; 好吧,您已经使用Commentlikes扩展了Comment,因此您可能希望在视图中使用该类“ Commentslikes”; not "Comment". 没有发表评论”。

Side notes: 旁注:

  • it's usually better to avoid plural in Model class names and better to use camel case. 通常最好避免在Model类名称中使用复数形式,而最好使用驼峰式大小写。
  • you could use composition instead. 您可以改用合成。

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

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