Hi there,
I noticed that the ModelAdmin displays a JSONField with a value of True
or False
in a inconsistent way.
Considering this minimal model:
class MyModel(models.Model):
data = models.JSONField()
used on a ModelAdmin like this:
@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
list_display = ("id", "data", "direct_data")
def direct_data(self, obj):
return obj.data
I populate the DB with two instances of it:
MyModel.objects.create(data=True)
MyModel.objects.create(data=False)
And what I see in the admin is this:
I would expect to see True
and False
in the “DATA” column, but its actually true
and False
. Digged a bit into the admin code, and found contrib.admin.utils.display_for_field
which has a check for the field being a JSONField. If that field value is truthy, it uses json.dumps
. But if not, it falls back to contrib.admin.utils.display_for_value
, which in the end returns str(value)
.
This explains, why for True
it renders true
(json.dumps version), but for False
it is False
(the str(value) version).
Would this be worth fixing / offering a PR?
Thanks!