I have these two models:
class CommonVehicle(models.Model):
year = models.ForeignKey(Year)
series = models.ForeignKey(Series)
engine = models.ForeignKey(Engine)
body_style = models.ForeignKey(BodyStyle)
...
class Vehicle(models.Model):
objects = VehicleManager()
stock_number = models.CharField(max_length=6, blank=False)
vin = models.CharField(max_length=17, blank=False)
common_vehicle = models.ForeignKey(CommonVehicle)
....
What I want to do is to have a count of how many times a given CommonVehicle
object is used in the Vehicle
class. So far my attempts are giving me one number, which is a total of all the records. How can I have the count being the total appearances for each CommonVehicle
Update 1 I was thinking something like this could work:
related_count = 0
for vehicle in vehicles:
related_count += Vehicle.objects.filter(common_vehicle=vehicle).count()