Why do I get different results if I use first() vs last() on a QuerySet with the length of 1

Viewed 1218

while writing a testcase for a method I discovered that I get different results if I use my_queryset.first().my_annotated_value than when I use my_queryset.last().my_annotated_value although my_queryset.count() returns 1.

Here is the relevant code snippet:

class ShopManager(models.Manager):

def get_best_matches(self, customer):

    shops = super(ShopManager, self).get_queryset().filter(employees__matches__customer=customer).annotate(max_match_percentage=Coalesce(Max('employees__matches__match_value'), 0)).order_by('-max_match_percentage')

    for shop in shops:
        shop.max_match_percentage = float(shop.max_match_percentage) * 100.0

    return shops

In the shell I run:

shops = Shop.objects.get_best_matches(customer=Customer_A)
shops.count() # returns 1

shops.first().max_match_percentage # returns 73.9843
shops.last().max_match_percentage # returns Decimal('0.739843')

I have different django apps for shops, matches, employees and customers.

I searched for hours and checked the implementation for first() and last() in the django docs. I could not find anything that explains this behavior.

Why are the values different, what exactly is happening? Am I doing something wrong or is this a bug?

1 Answers
Related