[Answered ]-How to get table data (including child table and sub child data) based on id which obtains from another table data? Django

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)

Leave a comment