How to automatically accept comments from authenticated users in django.contrib.comments

StackOverflow https://stackoverflow.com/questions/2553330

  •  23-09-2019
  •  | 
  •  

문제

Is there a way to make Django automatically set the is_public field of the comment as True.

I only allow comments for registered users and would like to skip manual review of comments posted.

도움이 되었습니까?

해결책 3

OK if anyone is looking for the answer for this, this is how I solved it:

# in models.py:
import datetime
def moderate_comment(sender, instance, **kwargs):
    if not instance.id:
        instance.is_public = True
from django.contrib.comments.models import Comment
from django.db.models import signals

signals.pre_save.connect(moderate_comment, sender=Comment)

다른 팁

The built in comments form should already set every comment to have is_public=True. See CommentDetailsForm.get_comment_create_data in http://code.djangoproject.com/browser/django/trunk/django/contrib/comments/forms.py

If you want to change this for logged in vs not logged in users, take a look at the built in comment moderation docs: http://docs.djangoproject.com/en/1.1/ref/contrib/comments/moderation/#ref-contrib-comments-moderation

You can write your own moderator which checks the comment to see if the comment.user is set and if it is do not moderate (is_public=True) otherwise set is_public=False.

overwrite the comments-form save-method, and set is_public to True

Overriding the moderate of CommentModerator works for me:

from django.contrib.comments.moderation import CommentModerator

class EntryModerator(CommentModerator):
    # [...]

    def moderate(self, comment, content_object, request):
        # If the user who commented is a staff member, don't moderate
        if comment.user and comment.user.is_staff:
            return False
        else:
            return True
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top