There is a model in Django that describes the history of a change in a certain value for each user over time.

 class UserValueHistory (models.Model): user = models.ForeignKey (...) value = models.FloatField (...) date = models.DateTimeField (...) 

The task is to get a sample containing users and a value corresponding to the maximum date for each user, using Django tools without using manual SQL writing.

    1 answer 1

    Write the class method UserValueHistory .

     @abstractclassmethod def get_latest_entry(cls): users = User.objects.all() result = [] for user in users: latest = UserValueHistory.objects.get(user=user).order_by['date'][0] result.append(latest) return result 
    • Unfortunately, this results in too many requests, as many as there are users. - Jenyay
    • This is a quick, head-on solution. - Balas