Repository: django/django
File: django/db/models/query.py (Python)

Code before:
        Return True if the QuerySet is ordered -- i.e. has an order_by()
        clause or a default ordering on the model (or is empty).
        """
        if isinstance(self, EmptyQuerySet):
            return True
        if self.query.extra_order_by or self.query.order_by:
            return True
        elif (
            self.query.default_ordering
            and self.query.get_meta().ordering
            and
            # A default ordering doesn't affect GROUP BY queries.
            not self.query.group_by
        ):
            return True
        else:
            return False

    @property
    def totally_ordered(self):
        """
        Returns True if the QuerySet is ordered and the ordering is
        deterministic. This requires that the ordering includes a field
        (or set of fields) that is unique and non-nullable.

        Returns False if the query involves a GROUP BY clause because the
        uniqueness of model fields does not apply to grouped results.
        Ordering specified via .extra(order_by=...) is ignored.
        """
        if isinstance(self, EmptyQuerySet):
            return True
        if not self.ordered:
            return False
        ordering = self.query.order_by
        if not ordering and self.query.default_ordering:
            ordering = self.query.get_meta().ordering
        if not ordering:
            return False
        opts = self.model._meta
        pk_fields = {f.attname for f in opts.pk_fields}
        ordering_fields = set()
        for part in ordering:
            # Search for single field providing a total ordering.
            field_name = None
            if isinstance(part, str):
                field_name = part.lstrip("-")
            elif isinstance(part, F):
                field_name = part.name
            elif isinstance(part, OrderBy) and isinstance(part.expression, F):
                field_name = part.expression.name
            if field_name:

Review comment (suggestion):
If you check the implementation of `ordered()`, you'll see that `GROUP BY` doesn't *always* cause the property to return False, but rather it only causes the default ordering of the model to be ignored.

Sorry if I was too categorical before, but we need to reword slightly.

Code after:
        Return True if the QuerySet is ordered -- i.e. has an order_by()
        clause or a default ordering on the model (or is empty).
        """
        if isinstance(self, EmptyQuerySet):
            return True
        if self.query.extra_order_by or self.query.order_by:
            return True
        elif (
            self.query.default_ordering
            and self.query.get_meta().ordering
            and
            # A default ordering doesn't affect GROUP BY queries.
            not self.query.group_by
        ):
            return True
        else:
            return False

    @property
    def totally_ordered(self):
        """
        Returns True if the QuerySet is ordered and the ordering is
        deterministic. This requires that the ordering includes a field
        (or set of fields) that is unique and non-nullable.

        For queries involving a GROUP BY clause, the model's default
        ordering is ignored. Ordering specified via .extra(order_by=...)
        is also ignored.
        """
        if not self.ordered:
            return False
        ordering = self.query.order_by
        if not ordering and self.query.default_ordering:
            ordering = self.query.get_meta().ordering
        if not ordering:
            return False
        opts = self.model._meta
        pk_fields = {f.attname for f in opts.pk_fields}
        ordering_fields = set()
        for part in ordering:
            # Search for single field providing a total ordering.
            field_name = None
            if isinstance(part, str):
                field_name = part.lstrip("-")
            elif isinstance(part, F):
                field_name = part.name
            elif isinstance(part, OrderBy) and isinstance(part.expression, F):
                field_name = part.expression.name
            if field_name:
                if field_name == "pk":
                    return True