My project has a "sort by last name" requirement, but we wanted to accomodate "funky" names. Here's what I did:
There are four fields in the database:
first_name
last_name
display_name
primary_email
All 3 name fields are optional. The email address field is required, but you could use a customer ID or username if that is more appropriate for your app.
The name are never accessed directly, except on the one form where you can edit these fields. In general, they are accessed by these two helper methods:
@property
def name(self):
if self.display_name:
return self.display_name
if self.first_name and self.last_name:
return self.first_name + ' ' + self.last_name
return self.primary_email
@property
def last_first(self):
if self.first_name and self.last_name:
return self.last_name + ', ' + self.first_name
if self.display_name:
return self.display_name
return self.primary_email
Then we do a culture specific case insensitive sort. This should cover most cases pretty well....... I hope :-)
There are four fields in the database:
All 3 name fields are optional. The email address field is required, but you could use a customer ID or username if that is more appropriate for your app.The name are never accessed directly, except on the one form where you can edit these fields. In general, they are accessed by these two helper methods:
Then we do a culture specific case insensitive sort. This should cover most cases pretty well....... I hope :-)