I have a very simple model which contains a JSONField
:
class Thing(models.Model):
title = models.CharField(max_length=1024)
text = JSONField(default=dict)
I've created a custom widget that allows for the input of key-value pairs:
class JsonWidget(forms.widgets.Widget):
template_name = 'json_widget.html'
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
data = json.loads(value)
if not data:
data = JSON_DEFAULT
context['data'] = data.items()
return context
def value_from_datadict(self, data, files, name):
keys = data.getlist('json-key')
values = data.getlist('json-value')
json_data = {k: v for k, v in zip(keys, values)}
return json_data
I coerce the dict
returned by the widget into a string in the field's clean
function on the form:
class ThingForm(forms.ModelForm):
class Meta:
model = Thing
fields = ['title', 'text']
widgets = {
'text': JsonWidget(),
}
def clean_text(self):
text = self.cleaned_data.get('text')
return json.dumps(text)
I've inspected the output of JsonWidget.value_from_datadict
(dict
) and ThingForm.clean_text
(str
) are the expected types. But when the object goes to save it throws an exception:
TypeError: the JSON object must be str, bytes or bytearray, not 'dict'
This is my first time building a custom widget for Django 1.11, is there something obvious I've missed here?
Thanks!