-
Notifications
You must be signed in to change notification settings - Fork 427
Description
I have a model that uses a pydantic SchemaField which allows me to validate incoming and outgoing JSON from a Django JSONField.
class Container(models.Model):
...
dimensions: DimensionsSchema = SchemaField()
I registered this model with auditlog, but when saving an instance of it auditlog (naturally) cannot serialize DimensionSchema
:
Traceback (most recent call last):
File "xxxxxxxxx.venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "xxxxxxxxx.venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxxxxxxx.venv\Lib\site-packages\rules\contrib\views.py", line 231, in _wrapped_view
return view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxxxxxxx.venv\Lib\site-packages\django\contrib\auth\decorators.py", line 60, in _view_wrapper
return view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxxxxxxx\core\views\containers.py", line 203, in edit_container
container.save()
File "xxxxxxxxx\core\models\containers.py", line 103, in save
return super().save(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxxxxxxx.venv\Lib\site-packages\django\db\models\base.py", line 891, in save
self.save_base(
File "xxxxxxxxx.venv\Lib\site-packages\django\db\models\base.py", line 977, in save_base
pre_save.send(
File "xxxxxxxxx.venv\Lib\site-packages\django\dispatch\dispatcher.py", line 189, in send
response = receiver(signal=self, sender=sender, **named)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxxxxxxx.venv\Lib\site-packages\auditlog\receivers.py", line 27, in wrapper
signal_handler(*args, **kwargs)
File "xxxxxxxxx.venv\Lib\site-packages\auditlog\receivers.py", line 59, in log_update
_create_log_entry(
File "xxxxxxxxx.venv\Lib\site-packages\auditlog\receivers.py", line 146, in _create_log_entry
raise error
File "xxxxxxxxx.venv\Lib\site-packages\auditlog\receivers.py", line 119, in create_log_entry
changes = model_instance_diff(
^^^^^^^^^^^^^^^^^^^^
File "xxxxxxxxx.venv\Lib\site-packages\auditlog\diff.py", line 185, in model_instance_diff
old_value = get_field_value(old, field)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "xxxxxxxxx.venv\Lib\site-packages\auditlog\diff.py", line 78, in get_field_value
value = json.dumps(value, sort_keys=True, cls=field.encoder)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\json_init.py", line 238, in dumps
**kw).encode(obj)
^^^^^^^^^^^
File "C:\Python312\Lib\json\encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\json\encoder.py", line 258, in iterencode
return _iterencode(o, 0)
^^^^^^^^^^^^^^^^^
File "xxxxxxxxx.venv\Lib\site-packages\django\core\serializers\json.py", line 106, in default
return super().default(o)
^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\json\encoder.py", line 180, in default
raise TypeError(f'Object of type {o.class.name} '
TypeError: Object of type DimensionsSchema is not JSON serializable
What would be the best approach to tell auditlog how to serialize such a field?