How to do a Django query from a ManyToMany relationship?

Viewed 23

So i have a table with vehicles in my DB, each vehicle has a relationship with an user in the Users table and each user has a company value. I need to get all the vehicles that have a user that has an specific compay value.

example: query = vehicles.users(company = company)
1 Answers

If the relationship between vehicles and users is ManyToMany, and the relationship between company and user is OneToOne, you could do:

query = vehicles.objects.filter(user__company__id="my_company_id")
Related