Tuesday, December 9, 2014

Get Last N Recent Django Model Instances

Problem:
We have our good old Django employee application and we want to show 5 recent joiners in the team on a Django template.

Solution:

Lets take the case of Employee instance having a created_date as one of the attribute that will define the datetime when the employee entered the system. We will follow the following approach:

  1. Get the queryset of django instance sorted in decending by created_date attribute
  2. Pull out top 5 or n from this list
  3. Please note the method in the sis a static method as it is not dependent on the instance but the Class itself
Code Sample:



 



1 comment:

  1. With various follow vendors transferring that will in-house built motion, could there be whatever dilemma that this transform could make this harder to your wrist watches to become maintained 20-50 yrs coming from at this point? Is going to foreign currency trading modify allow it to be very complicated to locate the keep an eye on serviced http://www.hotreplicaonline.com, simultaneously domestically as well as by the look at business enterprise them selves? Smaller makes typically are not assured to are present and even suppose one thing goes completely wrong, it is not necessarily transparent whether segments shall be attainable. At the same time, sometimes significant makes will frequently never assistance old different watches. A few might, nonetheless it is not guaranteed. Basically a single thing might be preset and maintained, but the a lot less frequent as well as earlier a watch is normally, the more problematic it is going to be to search for another person who are able to work with the item, and also the more expensive it may possibly get.

    ReplyDelete