djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Broken JSONField in admin page

Open pimuzzo opened this issue 3 years ago • 1 comments

I have a custom user model like this:

from djongo.models import JSONField

class User(AbstractUser):
    _id = ObjectIdField(primary_key=True)
    extra = JSONField(default=dict)

doesn't work on the admin page, in particular trying to make it show, results in an error in this Djongo's method because value is of type collections.OrderedDict instead of dict or list:

    def to_python(self, value):
        if not isinstance(value, (dict, list)):
            raise ValueError(
                f'Value: {value} stored in DB must be of type dict/list'
                'Did you miss any Migrations?'
            )
        return value

pimuzzo avatar May 23 '22 16:05 pimuzzo

Maybe you can extend the JSONField like bellow

# fields.py
from djongo.models import JSONField  # type: ignore

class MyJSONField(JSONField):
    """ do eval beforce save to mongo """
    def to_python(self, value):
        try:
            value = eval(value)
        except Exception as e:
            raise ValueError(
                f'Value: {value} invalid, make sure before submit?'
            )
        return super().to_python(value)
# models.py
from djongo import models
from .fields import MyJSONField

class CollectorPool(models.Model):
    ...
    extra_data = MyJSONField(blank=True, null=True, verbose_name='extra data')
# admin.py
from jsoneditor.forms import JSONEditor
from .fields import MyJSONField

@admin.register(CollectorPool)
class CollectorPoolAdmin(admin.ModelAdmin):
    ...
    formfield_overrides = {
        MyJSONField: {'widget': JSONEditor(attrs={'style': 'width: 620px;'})}
    }

in django admin image

in mongo image

hope this helps.

wuyue92tree avatar May 25 '22 07:05 wuyue92tree