Check if item is in ManyToMany field?

django, python

Solution

You can use the `in` operator:

if not request.user in thread.participants.all():
    ...

Problem

Consider this model: ``` class pm_thread(models.Model): subject = models.CharField(max_length=200) participants = models.ManyToManyField(User) ``` What would be the best way to check that a user is in the ManyToManyField? Example: ``` thread = get_object_or_404(pm_thread, pk=thread_id) if not thread.participants.contains(request.user): return HttpResponse("403 FORBIDDEN",status=403) ```

Original source