Repository: django/django
File: django/core/serializers/python.py (Python)

Code before:
    def _value_from_field(self, obj, field):
        if isinstance(field, CompositePrimaryKey):
            return [self._value_from_field(obj, f) for f in field]
        value = field.value_from_object(obj)
        # Protected types (i.e., primitives like None, numbers, dates,
        # and Decimals) are passed through as is. All other values are
        # converted to string first.
        return value if is_protected_type(value) else field.value_to_string(obj)

    def handle_field(self, obj, field):
        self._current[field.name] = self._value_from_field(obj, field)

    def handle_fk_field(self, obj, field):
        if self.use_natural_foreign_keys:
            natural_key_value = self._resolve_fk_natural_key(obj, field)
            if natural_key_value is not None:
                value = natural_key_value
            else:
                value = self._value_from_field(obj, field)
        else:
            value = self._value_from_field(obj, field)
        self._current[field.name] = value

    def handle_m2m_field(self, obj, field):
        if field.remote_field.through._meta.auto_created:
            use_natural = (
                self.use_natural_foreign_keys
                and self._model_supports_natural_key(field.remote_field.model)
            )

            def m2m_value(value):
                if use_natural:
                    natural_key_value = self._resolve_natural_key(value)
                    if natural_key_value is not None:
                        return natural_key_value

                return self._value_from_field(value, value._meta.pk)

            def queryset_iterator(obj, field):
                attr = getattr(obj, field.name)
                if use_natural:
                    chunk_size = (
                        2000 if getattr(attr, "prefetch_cache_name", None) else None
                    )
                    return attr.iterator(chunk_size)

                query_set = attr.select_related(None).only("pk")
                chunk_size = 2000 if query_set._prefetch_related_lookups else None
                return query_set.iterator(chunk_size=chunk_size)

            m2m_iter = getattr(obj, "_prefetched_objects_cache", {}).get(

Review comment (refactor):
Same here, it would be helpful to mix in fewer refactors here. Let's keep the if/else branches as they were and only insert the changes you need.

Code after:
        # Protected types (i.e., primitives like None, numbers, dates,
        # and Decimals) are passed through as is. All other values are
        # converted to string first.
        return value if is_protected_type(value) else field.value_to_string(obj)

    def handle_field(self, obj, field):
        self._current[field.name] = self._value_from_field(obj, field)

    def handle_fk_field(self, obj, field):
        if self.use_natural_foreign_keys and (
            natural_key_value := self._resolve_fk_natural_key(obj, field)
        ):
            value = natural_key_value
        else:
            value = self._value_from_field(obj, field)
        self._current[field.name] = value

    def handle_m2m_field(self, obj, field):
        if field.remote_field.through._meta.auto_created:
            if self.use_natural_foreign_keys and self._model_supports_natural_key(
                field.remote_field.model
            ):

                def m2m_value(value):
                    if natural := value.natural_key():
                        return natural
                    else:
                        return self._value_from_field(value, value._meta.pk)

                def queryset_iterator(obj, field):
                    attr = getattr(obj, field.name)
                    chunk_size = (
                        2000 if getattr(attr, "prefetch_cache_name", None) else None
                    )
                    return attr.iterator(chunk_size)

            else:

                def m2m_value(value):
                    return self._value_from_field(value, value._meta.pk)

                def queryset_iterator(obj, field):
                    query_set = getattr(obj, field.name).select_related(None).only("pk")
                    chunk_size = 2000 if query_set._prefetch_related_lookups else None
                    return query_set.iterator(chunk_size=chunk_size)

            m2m_iter = getattr(obj, "_prefetched_objects_cache", {}).get(
                field.name,
                queryset_iterator(obj, field),
            )
            self._current[field.name] = [m2m_value(related) for related in m2m_iter]