[Answered ]-Django: prefetch related with sort by runs multiple queries

1👍

You can use Prefetch:

prefetch = Prefetch('user_logging_details', queryset= UserActivity.objects.all().order_by('ip_address'))
users = User.objects.prefetch_related(prefetch).all()


for user in users:
    users_activity = user.user_logging_details.all()
    # it should return user activities ordered by ip address
👤ruddra

Leave a comment