Home > Software engineering >  Django - How to add a comment limit of 1
Django - How to add a comment limit of 1

Time:11-11

I currently have a comment functionality and I want to add a limit of one comment for each user but don't know how to do that. I tought on making a 'posted' field in the user model which would be true when the user posted a comment but I don't know how to do that and most importantly if that is the better way of doing that...

class Comment(models.Model):
    service = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True, related_name='comments')
    author = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True,)

    content = models.CharField(max_length=200, null=False, blank=True)
    date = models.DateTimeField(auto_now_add=True)

    def get_absolute_url(self):
        return reverse('product-feedback', kwargs={'pk': self.pk})
class ProductFeedbackView(DetailView):
    model = Product
    template_name = 'services/product-feedback.html'

    def get_context_data(self , **kwargs):
        data = super().get_context_data(**kwargs)
        connected_comments = Comment.objects.filter(service=self.get_object())
        number_of_comments = connected_comments.count()
        data['comments'] = connected_comments
        data['no_of_comments'] = number_of_comments
        data['comment_form'] = CommentForm()
        return data

    def post(self , request , *args , **kwargs):
        if self.request.method == 'POST':
            comment_form = CommentForm(self.request.POST)
            if comment_form.is_valid():
                content = comment_form.cleaned_data['content']

            new_comment = Comment(content=content, author=self.request.user , service=self.get_object())
            new_comment.save()
            return redirect(self.request.path_info)
{% block content %}

    {% if user.is_authenticated %}
        <form action="" method="POST" id="main_form" >
            <div>
                <label for="comment">Type Comment here</label>
                {{ comment_form.content }} {% csrf_token %} <input type="submit" value="Post"></div>
            </div>
        </form>
        {% else %}
            <h2>You need to Login to comment</h2> 
        {% endif %}

        {% for comment in comments %}
            <h3> <b>{{ comment.author }} : </b> {{ comment.content }}</h3> 
        {% endfor %}

{% endblock content %}

CodePudding user response:

If you are really sure a user should post one and only one comment, you should use a OneToOneField. This way, the unique constraint shall be be automatically generated and the object easier to manage.

class Comment(models.Model):
    service = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True, related_name='comments')
    author = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True,)
    content = models.CharField(max_length=200, null=False, blank=True)

If you want the user to post only one comment per product but still allow him to write comments on as many products as he wants, you should add a constraint in the Meta class :

class Comment(models.Model):
    service = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True, related_name='comments')
    author = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True,)
    content = models.CharField(max_length=200, null=False, blank=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=["service", "author"], name="One comment per user per product")
        ]
  • Related