Django Admin: JSONField default empty dict wont save in admin

前端 未结 4 1751
一向
一向 2021-02-19 11:42

in my model defn i have

from django.contrib.postgres.fields import JSONField
.....
.......
.........

media_data=JSONField(default=dict)

I crea

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 12:08

    This caused problems for me recently, though with django-mysql rather than postgres and in a custom ModelForm rather than the admin interface.

    I ended up overriding my model's save() method:

    from django_mysql.models import JSONField
    
    class yourModel(model):
        media_data=JSONField(default=dict, blank=True)
    
        def clean(self, *args, **kwargs):
            if self.media_data is None:
                self.media_data = "{}"
    
        def save(self, *args, **kwargs):
            self.clean()
            super().save(*args, **kwargs)
    

提交回复
热议问题