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

Code before:
                ordering_fields.add(field.attname)

        # Account for members of a CompositePrimaryKey.
        if ordering_fields.issuperset(pk_fields):
            return True
        # No single total ordering field, try unique_together and total
        # unique constraints.
        constraint_field_names = (
            *opts.unique_together,
            *(constraint.fields for constraint in opts.total_unique_constraints),
        )
        for field_names in constraint_field_names:
            # Normalize attname references by using get_field().
            try:
                fields = [opts.get_field(field_name) for field_name in field_names]
            except exceptions.FieldDoesNotExist:
                continue
            # Composite unique constraints containing a nullable column
            # cannot ensure total ordering.
            if any(field.null for field in fields):
                continue
            if ordering_fields.issuperset(field.attname for field in fields):
                return True
        # If no set of unique fields is present in the ordering, rely on the
        # primary key to provide total ordering. Since this property is
        # introspective, we return False if the PK is not already present.
        return False

    @property
    def db(self):
        """Return the database used if this query is executed now."""
        if self._for_write:
            return self._db or router.db_for_write(self.model, **self._hints)
        return self._db or router.db_for_read(self.model, **self._hints)

    ###################
    # PRIVATE METHODS #
    ###################

    def _insert(
        self,
        objs,
        fields,
        returning_fields=None,
        raw=False,
        using=None,
        on_conflict=None,
        update_fields=None,
        unique_fields=None,
    ):
        """

Review comment (suggestion):
code comments avoid "We", but actually we can chop this one, since this comment only applied to the admin_views use case for returning a queryset instead of a boolean.



Code after:
                    return True
                if field.unique and not field.null:
                    return True
                ordering_fields.add(field.attname)

        # Account for members of a CompositePrimaryKey.
        if ordering_fields.issuperset(pk_fields):
            return True
        # No single total ordering field, try unique_together and total
        # unique constraints.
        constraint_field_names = (
            *opts.unique_together,
            *(constraint.fields for constraint in opts.total_unique_constraints),
        )
        for field_names in constraint_field_names:
            # Normalize attname references by using get_field().
            try:
                fields = [opts.get_field(field_name) for field_name in field_names]
            except exceptions.FieldDoesNotExist:
                continue
            # Composite unique constraints containing a nullable column
            # cannot ensure total ordering.
            if any(field.null for field in fields):
                continue
            if ordering_fields.issuperset(field.attname for field in fields):
                return True

        return False

    @property
    def db(self):
        """Return the database used if this query is executed now."""
        if self._for_write:
            return self._db or router.db_for_write(self.model, **self._hints)
        return self._db or router.db_for_read(self.model, **self._hints)

    ###################
    # PRIVATE METHODS #
    ###################

    def _insert(
        self,
        objs,
        fields,
        returning_fields=None,
        raw=False,
        using=None,
        on_conflict=None,
        update_fields=None,
        unique_fields=None,
    ):