Testing django.tasks and understanding json Encoding

I have been recently studying the new django.tasks framework and testing a migration in my own project. At a high level, it is generally working great! and is letting me simplify some code in my projects. There are however some gotcha around certain types that are not technically bugs in django.tasks, but I wonder if they could be handled slightly nicer

For reference we need to know a few areas

normalize_json ( django/django/utils/json.py at 6.0 · django/django · GitHub ) is called from various places in preparation for saving a Task to be executed

Django provides a built in DjangoJSONEncoder ( django/django/core/serializers/json.py at 6.0 · django/django · GitHub ) though for many areas like JSONField it actually defaults to the default Python json.Encoder

A fairly common pattern for tasks, is to do extra processing on a model after creation, so we might have something like this

``python
@receiver(post_save, sender='myapp.MyModel')
def post_save_tasks(instance, **kwargs):
my_task.enqueue(instance.pk)
```

If our instance uses any of the integer keys, we’re fine, but if we happen to use UUIDField, we will get json encoding errors.

DjangoJSONEncoder handles a few more types like uuid.UUID and Promise that seem like would be useful for normalize_json could also handle.

2 Likes

Originally started as a thread on Mastodon

https://elk.zone/social.tsun.co/@kfdm/115863244456507659

With some other testing from my blog

Good question. This was discussed extensively before merge, and the consensus was we could consider a new feature adding a TASK_SERIALIZER setting for swappable serializers.

What we need now is a ticket at django/new-features showing the use case and musing on the interface. Are you up for that? :wrapped_gift:

1 Like

Jake replied here with some ideas for where to take this.

1 Like