[Answer]-Listing all related objects and allow paging on related objects

1đź‘Ť

âś…

This is what select_related is designed for:

Returns a QuerySet that will automatically “follow” foreign-key
relationships, selecting that additional related-object data when it
executes its query. This is a performance booster which results in
(sometimes much) larger queries but means later use of foreign-key
relationships won’t require database queries.

In your case it would be:

Group.objects.select_related().get(pk=group) 

Now on each FK lookup, you won’t hit the database again.

The next step would be to cache the results using the cache api so that you don’t hit the database everytime the next “page” is called. This would be useful if your data isn’t time sensitive.

👤Burhan Khalid

Leave a comment