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

JSON null literal value cannot be set with auditlog registered models #645

Closed
jforsman opened this issue May 17, 2024 · 4 comments · Fixed by #646
Closed

JSON null literal value cannot be set with auditlog registered models #645

jforsman opened this issue May 17, 2024 · 4 comments · Fixed by #646
Labels

Comments

@jforsman
Copy link

jforsman commented May 17, 2024

This is regression in 3.0.0 version, was working in 2.2.2.
Tested with:
Django version 4.2.11
Python 3.11.9

All you need to have a is model with JSONField and then register it:

class ALModel(models.Model):
    id = models.BigAutoField(primary_key=True)
    json_content = models.JSONField(
        verbose_name="Content",
        blank=True,
        null=True,
        help_text="Some json content",
    )


auditlog.register(ALModel)

and then try to create (or update) json_content field with Value(None, JSONField).

Made a small django app do demonstrate this: https://github.com/jforsman/altest

@jforsman
Copy link
Author

And the traceback:
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/django/db/models/query.py", line 658, in create
obj.save(force_insert=True, using=self.db)
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/django/db/models/base.py", line 814, in save
self.save_base(
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/django/db/models/base.py", line 892, in save_base
post_save.send(
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 176, in send
return [
^
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 177, in
(receiver, receiver(signal=self, sender=sender, **named))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/auditlog/receivers.py", line 27, in wrapper
signal_handler(*args, **kwargs)
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/auditlog/receivers.py", line 40, in log_create
_create_log_entry(
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/auditlog/receivers.py", line 146, in _create_log_entry
raise error
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/auditlog/receivers.py", line 119, in _create_log_entry
changes = model_instance_diff(
^^^^^^^^^^^^^^^^^^^^
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/auditlog/diff.py", line 186, in model_instance_diff
new_value = get_field_value(new, field)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jforsman/dev/altest/altest/venv/lib/python3.11/site-packages/auditlog/diff.py", line 78, in get_field_value
value = json.dumps(value, sort_keys=True, cls=field.encoder)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jforsman/.pyenv/versions/3.11.9/lib/python3.11/json/init.py", line 238, in dumps
**kw).encode(obj)
^^^^^^^^^^^
File "/home/jforsman/.pyenv/versions/3.11.9/lib/python3.11/json/encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jforsman/.pyenv/versions/3.11.9/lib/python3.11/json/encoder.py", line 258, in iterencode
return _iterencode(o, 0)
^^^^^^^^^^^^^^^^^
File "/home/jforsman/.pyenv/versions/3.11.9/lib/python3.11/json/encoder.py", line 180, in default
raise TypeError(f'Object of type {o.class.name} '
TypeError: Object of type Value is not JSON serializable

@hramezani
Copy link
Member

Thanks @jforsman for reporting this bug. I created #646 to fix the problem

@hramezani hramezani added the bug label May 22, 2024
@jforsman
Copy link
Author

Any idea in which version it will be released?

@hramezani
Copy link
Member

it will be included in the next release

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

Successfully merging a pull request may close this issue.

2 participants