limit the instances created by the current logged in user where the user is a foreign key in the model.

14 views
Skip to first unread message

Saloni Kalra

unread,
Jul 1, 2018, 12:26:12 PM7/1/18
to django...@googlegroups.com
I have a feedback app in which the user signs in, logs in and gives feedback. i wish to limit the number of feedbacks a user can give. how to do this and where? shall I user validation?

Tomasz Knapik

unread,
Jul 1, 2018, 12:52:55 PM7/1/18
to django...@googlegroups.com, Saloni Kalra

You probably have to validate it yourself. You probably should do this at the form level. I don't think there's a smart way to do that in Django.

1. Pass user into the form's __init__.

2. Check the number of user's feedback in the form's clean() method and raise ValidationError if user has left enough feedback.

class FeedbackForm(forms.Form):
    def __init__(self, user, *args, **kwargs):
        self.user = user

    def clean(self):
        super().clean()
        if Feedback.objects.filter(user_id=self.user.pk).count() >= 3:
            raise ValidationError(['You have already left feedback 3 times.'])

======================

form = FeedbackForm(request.user, request.POST)

On 01/07/18 17:25, Saloni Kalra wrote:
I have a feedback app in which the user signs in, logs in and gives feedback. i wish to limit the number of feedbacks a user can give. how to do this and where? shall I user validation?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMO5YArF_cYq_-E2Zw0COTVFW20x5H6k4eo700i0ULD19BX9cQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Saloni Kalra

unread,
Jul 1, 2018, 1:27:12 PM7/1/18
to Django users
thanks much!
Reply all
Reply to author
Forward
0 new messages