how to verify if object exist in manytomany

I advise you to use:

if beer.salas_set.filter(pk=sala.pk).exists():
    # do stuff

If you use sala in beer.salas_set.all() instead, it selects all records from the relation table and loops over them to find, whether the given object is there or not. However, beer.salas_set.filter(pk=sala.pk).exists() only selects zero or one row from the database and immediately gives the result (without looping).

Leave a Comment