django-forms:允许登录用户每个人只提交一个评论 post

django-forms: allow logged in user to submit only one comment per individual post

我有一个模型 Post,用户可以在其中发表评论以及一组评分。我想将用户评论限制为每 post 一条。我在设置时遇到问题

型号

class Comment(models.Model):
    post = models.ForeignKey(Post, related_name="comments")
    user = models.ForeignKey(User, related_name="usernamee")
    ...

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile')

class Post(models.Model):
...

观看次数

def add_comment(request, slug):
    post = get_object_or_404(Post, slug=slug)

# I tried wrapping all of the below in an "if" statement, something like
# if request.user.comment.exists(): to check if the user has already
# left a comment on this specific post, but I'm not sure of the right way to perform such a check here.

    if request.method == 'POST':
            form = CommentForm(request.POST or None)
            if form.is_valid():
                comment = form.save(commit=False)
                comment.post = post 
                comment.user = request.user 
                comment.email = request.user.email
                comment.picture = request.user.profile.profile_image_url()

                comment.save()

                messages.success(request, "Thank you for leaving a review!")
                return redirect('blog:post_detail', slug=post.slug)

            else:
                messages.error(request, "Something went wrong! We weren't able to process your review :(")  
        else:
            form = CommentForm()



        template = "blog/post/add_comment.html"
        context = {

            'form': form,
            'post': post,
            #'comment_count': comment_count

            }
        return render(request, template, context)

我的印象是,我所需要的只是将我的 add_comment 视图中的整个表单代码包装在某种验证系统中,以检查当前登录的用户是否已经对该特定内容发表了评论 post(查看视图中的评论)

有人知道潜在的解决方案是什么吗?或者如果我这样做是正确的?

一个可能的解决方案是:

  1. 获取视图中的当前 Post 条评论
  2. 按您的 request.user
  3. 过滤评论
  4. 查看查询是否返回非空查询集

类似于:

from django.core.exceptions import PermissionDenied


def add_comment(request, slug):
    post = get_object_or_404(Post, slug=slug)

    # Get the comments posted by the user for this post
    user_comments = post.comments.filter(user=request.user)

    if request.method == 'POST':
            form = CommentForm(request.POST or None)

            # Check if there are any comments posted by the user
            if user_comments:
                # If there are any, raise an error
                raise PermissionDenied('You have already commented on this post.') 

            if form.is_valid():
                comment = form.save(commit=False)
                comment.post = post 
                comment.user = request.user 
                comment.email = request.user.email
                comment.picture = request.user.profile.profile_image_url()

                comment.save()

                messages.success(request, "Thank you for leaving a review!")
                return redirect('blog:post_detail', slug=post.slug)

            else:
                messages.error(request, "Something went wrong! We weren't able to process your review :(")  
    else:
        form = CommentForm()

    template = "blog/post/add_comment.html"
    context = {
        'form': form,
        'post': post
    }

    return render(request, template, context)

当然可以修改这里的报错,但是主要思路是获取评论,通过request.user过滤,看有没有