I have the following Django models (greatly simplified here):
class Author:
name = models.TextField()
class Editor:
name = model.TextField()
class Publication:
authors = models.ManyToManyField(Author)
editors = models.ManyToManyField(Editor)
A publication can have either authors or editors, but not both. I would like to order a the list of publications by the unification of authors and editors, alphabetically sorted. In other words, I would like to order by a virtual field, let's call it creators
which is the concatenation of authors and editors.
The query:
Publication.objects.all().order_by('authors__name', 'editors__name')
... will clump together the publications that have NO authors, and within that group sort according to editors, which is NOT what I want. Effectively, it should use whatever of authors or editors is available for a publication and sort by that.
This ordering has to happen at the database level, the result sets can be huge.