﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34822	Provide migration serializer for `_lru_cache_wrapper`	Natalia Bidart	Nick Pope	"Following ticket #24561, and [https://forum.djangoproject.com/t/callables-for-model-fields-choices/23654 this forum post], it would be useful to have the migrations serializer knowing how to serialize the `_lru_cache_wrapper` type which is returned when calling the `functools.cache` and `functools.lru_cache` decorators.

An example of a use case would be a callable defined for either model field's `choices` or `default` that perform an expensive calculation (either CPI or IO bound), and such callable is wrapped with a caching decorator. For instance:

{{{
#!python
 import functools


    @functools.cache
    def fib(n):
        if n < 2:
            return n
        return fib(n - 1) + fib(n - 2)


    @functools.cache
    def get_choices():
        return {i: fib(i) for i in range(20)}


    class Spiral(models.Model):
        size = models.IntegerField(choices=get_choices)
}}}

Currently, the error that is raised when generating migrations (assuming support for callable `choices`), is:

{{{
ValueError: Cannot serialize: <functools._lru_cache_wrapper object at 0x7f819ab3b270>
}}}

Lastly, while I do appreciate this could be done with a custom serializer, I think it would be of value to provide this in Django core since using the mentioned the decorators is not (IMHO) a niche case."	New feature	closed	Migrations	dev	Normal	fixed	serializer functools cache		Ready for checkin	1	0	0	0	0	0
