Let's say I have this Person class that consist of last_name string field.
I would like to display links of all first letters of names existing in db.
So for example:
A B D E... when there's Adams, Brown, Douglas, Evans and no one that last_name starts with C.
Of course view is not a problem here as I want to prepare all of this on backend. So the question is how to write good model's or view's function that will provide this.
I would like it to be DB-independent, however tricks for any particular DB would be a bonus.
Also, I would like this to be quite well optimized in terms of speed because it could be a lot of names. It should be less naive than this most simple algorithm:
- Get all people
- Create set (because of the uniqueness of elements) of the first letters
- Sort and return
So for example (in views.py):
names = Person.objects.values_list('last_name', flat=True)
letters = {name[0] for name in names}
letters_sorted = sorted(letters)
Of course I could order_by first or assign new attribute to each objects (containing first letter) but I don't think it will speed up the process.
I also think that assuming that all letters are in use is bad assumption if I would go check each letter if at least one name for this letter exists ;-)
Which approach would be best effective for databases and django here?