Views
company = Company.objects.get(id = company_id) # getting input from django urls (<int:company_id>)
vehicles = CompanyContainVehicles.objects.filter(company_id=company.id) # Give all rows having same id (company.id)
all_vehicles = Vehicles.objects.filter(id=vehicles.vehicle_id) # Not Working
How do I get data from tables whose multiple id's obtained by another table?
Models
class Company(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
slug = models.SlugField(blank=True, null=True, unique=True)
description = models.TextField()
class Vehicles(models.Model):
id = models.AutoField(primary_key=True)
vehicle_number = models.IntegerField()
name = models.CharField(max_length=255)
slug = models.SlugField(blank=True, null=True, unique=True)
class CompanyContainVehicles(models.Model):
id = models.AutoField(primary_key=True)
company_id = models.ForeignKey(Company, on_delete=models.CASCADE)
vehicle_id = models.ForeignKey(Vehicles, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True, blank=True)
Above are my table details and I need to get all vehicles from table Vehicles which is obtained from CompanyContainVehicle table (that define which company seel out which vehicle) based on company_id that obtain from table Company which contains details of companies.