I’m trying to use some functions with lru_cache decorator to set the on_delete option in a relation to avoid some database hits.
from functools import lru_cache
@lru_cache
def get_deleted_user():
return User.objects.get(pk=DELETED_USER)
class Sale(models.Model):
...
client = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET(get_deleted_user),
related_name='sales',
)
...
When I tried makemigrations Django says that can not serialize lru_cache into migration files.
I change the model and use the ‘deleted’ user pk directly and it works.
class Sale(models.Model):
...
on_delete=models.SET(DELETED_USER),
I’m not sure if its the right approach but I wonder if exist a way to work with lru_cache in migration files.