1π
β
I think I will give all the data in a variable and we should divide it and add with their
Vehicle
.
You donβt have to. Django can read ForeignKey
relations in reverse. You can query with:
qs = Vehicle.objects.prefetch_related('vehiclepart_set')
then you can enumerate over the queryset, and for each Vehicle
object, access this with .vehiclepart_set.all()
. For example:
for item in qs:
print(vehicle_name)
for part in item.vehiclepart_set.all():
print(part.id)
Source:stackexchange.com