Open pimuzzo opened 2 years ago
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
in mongo
hope this helps.
I have a custom user model like this:
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 typecollections.OrderedDict
instead ofdict
orlist
: