# Serialize lru\_cache in migration files

**URL:** <https://forum.djangoproject.com/t/serialize-lru-cache-in-migration-files/20237>\
**Category:** Using the ORM\
**Created:** [April 14, 2023, 2:44am UTC](https://forum.djangoproject.com/t/serialize-lru-cache-in-migration-files/20237 "2023-04-14T02:44:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcovelarde](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcovelarde/32/4087_2.png) [@marcovelarde](https://forum.djangoproject.com/u/marcovelarde)\
**Post date:** [April 14, 2023, 2:44am UTC](https://forum.djangoproject.com/t/serialize-lru-cache-in-migration-files/20237/1 "2023-04-14T02:44:14Z")

</div>

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.

```py
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.

```py
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.

---

<div class="post-metadata">

**Author:** ![DanielGnzlzVll](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/danielgnzlzvll/32/4585_2.png) [@DanielGnzlzVll](https://forum.djangoproject.com/u/DanielGnzlzVll)\
**Post date:** [April 14, 2023, 4:16am UTC](https://forum.djangoproject.com/t/serialize-lru-cache-in-migration-files/20237/2 "2023-04-14T04:16:39Z")

</div>

as far as i know, the value you are providing in sent to the db, so it needs to be serializable so db can save it. it doesn’t makes sense to make yo use a python function for that. so i would say you approach is correct

---

<div class="post-metadata">

**Author:** ![marcovelarde](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/marcovelarde/32/4087_2.png) [@marcovelarde](https://forum.djangoproject.com/u/marcovelarde)\
**Post date:** [April 15, 2023, 3:18am UTC](https://forum.djangoproject.com/t/serialize-lru-cache-in-migration-files/20237/3 "2023-04-15T03:18:51Z")

</div>

I use a function because django recommend that and if for some reason I need to change that behavior, I only update the function logic and doesn’t generate a migration file. In this case, I don’t think that will happen so as you say I’m going to use the pk directly.  
Thanks.
