django-pghistory icon indicating copy to clipboard operation
django-pghistory copied to clipboard

Object of type UUID is not JSON serializable

Open sanjiv-wal opened this issue 2 years ago • 3 comments

We are using uuid as primary key for user in django. When using UUID, django admin login failing due to django-pghistory.

sanjiv-wal avatar May 18 '22 14:05 sanjiv-wal

When I used model_util's UUIDField, it failed. Thus I switched to use Django's models.UUIDField, it worked.

xjlin0 avatar May 20 '22 19:05 xjlin0

@xjlin0 I am using Django's models.UUIDField only.

sanjiv-wal avatar May 20 '22 20:05 sanjiv-wal

Here is my settings with serialize=False, hope it work

import pghistory
from django.db import models

class MyModel(models.Model):
    id = models.UUIDField(default=uuid4, editable=False, primary_key=True, serialize=False)

class MyModelsHistory(pghistory.get_event_model(
    MyModel,
    pghistory.Snapshot('mymodel.snapshot'),
    name='MyModelsHistory',
    related_name='history',
)):
    id = models.UUIDField(db_index=True, default=uuid4, editable=False, serialize=False)
    pgh_id = models.BigAutoField(primary_key=True, serialize=False)
    pgh_created_at = models.DateTimeField(auto_now_add=True)
    pgh_label = models.TextField(help_text='The event label.')
    pgh_obj = models.ForeignKey(db_constraint=False, on_delete=models.deletion.DO_NOTHING, related_name='history', to='myapp.mymodel')
    pgh_context = models.ForeignKey(db_constraint=False, null=True, on_delete=models.deletion.DO_NOTHING, related_name='+', to='pghistory.context')

xjlin0 avatar May 21 '22 13:05 xjlin0

This was addressed in PR #47 and released in version 2.1.0!

jzmiller1 avatar Aug 27 '22 16:08 jzmiller1

The default JSON serializer is now the one provided by Django, plus you can swap in your own with settings.PGHISTORY_JSON_ENCODER thanks to @jzmiller1 !

wesleykendall avatar Sep 06 '22 01:09 wesleykendall