Home > Blockchain >  How to order by last name on a full name column in Django?
How to order by last name on a full name column in Django?

Time:01-29

I have a model contact with a column called FullName which contains, for example, "John Smith".

How can order the data by the last name that appears in the FullName column?

For a long name like "John Smith Doe", I would like to order the data by the word "Doe". I am using postgres db

CodePudding user response:

class Model(models.Model):
    @property
    def last_name(self):
      return full_name.split()[-1]

ans = sorted(Model.objects.all(), key=lambda ans: ans.last_name)
  •  Tags:  
  • Related