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

Code before:
            return {}
        opts = self.model._meta
        unique_fields = [
            constraint.fields[0]
            for constraint in opts.total_unique_constraints
            if len(constraint.fields) == 1
        ]
        if (
            field_name != "pk"
            and not opts.get_field(field_name).unique
            and field_name not in unique_fields
            and self.query.distinct_fields != (field_name,)
        ):
            raise ValueError(
                "in_bulk()'s field_name must be a unique field but %r isn't."
                % field_name
            )

        qs = self

        def get_obj(obj):
            return obj

        if issubclass(self._iterable_class, ModelIterable):
            # Will raise an unhelpful AttributeError if field_name is deferred
            # We could catch this earlier and raise a TypeError
            get_key = operator.attrgetter(field_name)

        elif issubclass(self._iterable_class, ValuesIterable):
            if field_name not in self.query.values_select:
                qs = qs.values(field_name, *self.query.values_select)

                def get_obj(obj):  # noqa: F811
                    # We can safely mutate the dictionaries returned by
                    # ValuesIterable here, since they are limited to the scope
                    # of this function, and get_key runs before get_obj.
                    del obj[field_name]
                    return obj

            get_key = operator.itemgetter(field_name)

        elif issubclass(self._iterable_class, ValuesListIterable):
            try:
                field_index = self.query.values_select.index(field_name)
            except ValueError:
                # field_name is missing from values_select, so add it.
                field_index = 0
                if issubclass(self._iterable_class, NamedValuesListIterable):
                    kwargs = {"named": True}
                else:
                    kwargs = {}

Review comment (suggestion):
```suggestion
            # Raise an AttributeError if field_name is deferred.
```

Code after:
            return {}
        opts = self.model._meta
        unique_fields = [
            constraint.fields[0]
            for constraint in opts.total_unique_constraints
            if len(constraint.fields) == 1
        ]
        if (
            field_name != "pk"
            and not opts.get_field(field_name).unique
            and field_name not in unique_fields
            and self.query.distinct_fields != (field_name,)
        ):
            raise ValueError(
                "in_bulk()'s field_name must be a unique field but %r isn't."
                % field_name
            )

        qs = self

        def get_obj(obj):
            return obj

        if issubclass(self._iterable_class, ModelIterable):
            # Raise an AttributeError if field_name is deferred.
            get_key = operator.attrgetter(field_name)

        elif issubclass(self._iterable_class, ValuesIterable):
            if field_name not in self.query.values_select:
                qs = qs.values(field_name, *self.query.values_select)

                def get_obj(obj):  # noqa: F811
                    # We can safely mutate the dictionaries returned by
                    # ValuesIterable here, since they are limited to the scope
                    # of this function, and get_key runs before get_obj.
                    del obj[field_name]
                    return obj

            get_key = operator.itemgetter(field_name)

        elif issubclass(self._iterable_class, ValuesListIterable):
            try:
                field_index = self.query.values_select.index(field_name)
            except ValueError:
                # field_name is missing from values_select, so add it.
                field_index = 0
                if issubclass(self._iterable_class, NamedValuesListIterable):
                    kwargs = {"named": True}
                else:
                    kwargs = {}
                    get_obj = operator.itemgetter(slice(1, None))