Python 如何扩展django注释模型
(对不起,我英语不好,我是一个蹩脚的法国人) 我尝试扩展django评论框架,以添加一个like/dislike系统。 阅读后,我已将此添加到我的model.py:Python 如何扩展django注释模型,python,django,extend,Python,Django,Extend,(对不起,我英语不好,我是一个蹩脚的法国人) 我尝试扩展django评论框架,以添加一个like/dislike系统。 阅读后,我已将此添加到我的model.py: from django.contrib.comments.models import Comment class Commentslikes(Comment): positif = models.IntegerField(default=0) negatif = models.IntegerField(defaul
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创建了带有3列的commentslikesmysql表:comment\u ptr\u id、positif、negatif。没关系
在我的view.py文件中,我用以下内容覆盖了comment post视图:
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”修改,但未在注释id为positif为20、否定if为10的commentslikes表中添加任何行。注释的非行已添加在注释类中
我忘记或做了什么
谢谢,
托马斯你好,托马斯
您已经用Commentslikes扩展了Comment,所以您可能希望在视图中使用该类“Commentslikes”;不是“评论”
旁注:
- 通常最好避免模型类名中的复数形式,最好使用camel大小写
- 你可以用构图代替