I need to do some custom sorting on a Member model which looks like this (simplified):
class User():
username = models.CharField()
# ...
class Member():
user = models.ForeignKey(User) # not required
invite_email = models.EmailField()
# ...
I need to sort my queryset of Member in the following way:
- Members that do not have a user set come last;
- Sort by user username alphabetically (case insensitive), or invite_email if Member does not have a user.
I can do the appropriate sorting with django:
queryset.order_by(Lower('user__username').asc(nulls_last=True), 'invite_email')
I thought that I could override the order_by method to apply my custom ordering.
My problem comes with DRF cursor pagination. Reading from the doc, it seems like the ordering should be a string, or an unchanging value generally speaking. But when I use the Lower expression to order my queryset as needed, the ordering received in the cursor pagination class is an OrderBy object. Doing so, the paginate_queryset method is unusable.
I find it weird that you can't apply a custom filtering with the DRF pagination classes. Am I missing something?
Thanks in advance for any tips you might have!