Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use with pydantic SchemaField #691

Open
LaundroMat opened this issue Dec 10, 2024 · 0 comments
Open

Use with pydantic SchemaField #691

LaundroMat opened this issue Dec 10, 2024 · 0 comments

Comments

@LaundroMat
Copy link

LaundroMat commented Dec 10, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant