Sunday, July 1, 2018

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

thanks much!

On Sunday, 1 July 2018 21:52:55 UTC+5, Tomasz Knapik wrote:

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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/7fd8bd0a-39bd-4d89-802a-a69419c6b127%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment