#34822: Provide migration serializer for `_lru_cache_wrapper`
-------------------------------------+-------------------------------------
Reporter: Natalia | Owner: nobody
Bidart |
Type: New | Status: new
feature |
Component: | Version: dev
Migrations | Keywords: serializer
Severity: Normal | functools cache
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/34822>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018a75ed53fa-9c2e5a34-23ce-4204-87f7-68c22af6d4d3-000000%40eu-central-1.amazonses.com.