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

Code before:
        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.

        Return 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 self.query.group_by:
            return False
        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
                # Normalize attname references by using get_field().
                try:
                    field = opts.get_field(field_name)

Review comment (refactor):
Sorry, on second look I think this logic is already covered by the call to `if not self.ordered`, so we can just remove this.
```suggestion
```

Code after:
            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
                # Normalize attname references by using get_field().
                try:
                    field = opts.get_field(field_name)
                except exceptions.FieldDoesNotExist: