'How to override mongoengine's QuerySet method?

How can I override a mongoengine's queryset method?

Specifically, I want to override .order_by(), but the closest I can get is to add another method ordered that would conditionally call .order_by():

class TransactionQuerySet(QuerySet):

    def ordered(self, *args, target_data=None, **kwargs):
        if target_data is None:
            result = self.order_by(*args, **kwargs)
            return result
        else:
            return 'transactions sorted by target data'

Ideally I would like this new method to be named the same as mongoengine's method - order_by - but if I do this I will exceed recursion depth when the queryset manager is called like Transaction.objects.order_by.

How can I do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source