SoFunction
Updated on 2024-10-29

django get field max, latest record operation

If it is pulled directly from the database

from import Max

().aggregate(Max('rating'))

If fetching from a list of existing mods

from  import Max
args = () 
(Max('rating'))

Get the latest row of data

max_rated_entry = ('rating')

Or specify get_latest_by in the model in advance.

from  import models

class YourModel():
  .....
  class Meta:
    get_latest_by = 'rating'

# When it's time to use
max_rated_entry = ()
return max_rated_entry.details

Additional knowledge: django how to get the name and verbose_name of a field based on a model

Django can be based on the incoming model to get the corresponding field name and verbosname, easy to use a unified template to call the

def get_headers_from_model(modelClass):
  '''
  Get the field name, display name of the object based on the model class object
  :param modelClass.
  :return.
  '''
  cols = []
  headers = []
  for f in modelClass._meta.get_fields():
    try:
      (  )
      ( f.verbose_name )
    except Exception as ex:
      pass
  return headers,cols

Above this django get the maximum value of the field, the latest record operation is all that I have shared with you, I hope to give you a reference, and I hope you support me more.