[Answer]-How to filter in many-to-many relationship in Django

1👍

For a start, there is no point in using __in with a single element which you then wrap in a list. The fact that you had to do that should have given you a hint that you are using the wrong predicate: just use the default (which is __eq, but since it’s the default you can leave it out altogether).

Also, if you only want to know if an object exists, use .exists() rather than .count(), as the latter is a more expensive query if there can be multiple objects.

But basically your problem is simple. You want to filter the times belonging to a schedule: so, start with that schedule, not the whole Schedule model.

exists = self.times.filter(time_arrival=time.time_arrival).exists()

Leave a comment