[Django] #27492: JsonResponse can't handle model_to_dict() with ImageField

96 views
Skip to first unread message

Django

unread,
Nov 16, 2016, 6:22:26 AM11/16/16
to django-...@googlegroups.com
#27492: JsonResponse can't handle model_to_dict() with ImageField
-------------------------------------+-------------------------------------
Reporter: Komarov | Owner: nobody
Valentin |
Type: Bug | Status: new
Component: HTTP | Version: 1.10
handling | Keywords: jsonresponse
Severity: Normal | model_to_dict imagefield
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have a model:

{{{#!python
class Item(models.Model):

title = models.TextField(verbose_name='Default title')
photo = models.ImageField(
verbose_name='Photo',
upload_to='media/',
blank=True,
)

def __str__(self):
return self.title
}}}

I am trying to serialize this model object, to answer as json to frontend.

{{{#!python
from app.models import Item
from django.forms.models import model_to_dict
from django.http import JsonResponse


def get(self, request, *args, **kwargs):
item_object = Item.objects.first()
to_json = model_to_dict(item_object)
return JsonResponse(to_json)
}}}

If my item.photo is empty (I haven't download photo yet), JsonResponse
throw TypeError: <ImageFieldFile: None> is not JSON serializable

{{{
[16/Nov/2016 13:51:15] "GET /items/?item_id=9&shop_id=4 HTTP/1.1" 500
129160
Internal Server Error: /items/
Traceback (most recent call last):
File "/home/lamberk/python/picasel/rc_cross_upsell/venv/lib/python3.5
/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/home/lamberk/python/picasel/rc_cross_upsell/venv/lib/python3.5
/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/lamberk/python/picasel/rc_cross_upsell/venv/lib/python3.5
/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "/home/lamberk/python/picasel/rc_cross_upsell/venv/lib/python3.5
/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/lamberk/python/picasel/rc_cross_upsell/venv/lib/python3.5
/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File
"/home/lamberk/python/picasel/rc_cross_upsell/CrossUpsell/apps/items/views.py",
line 54, in get
'up_sells': up_sells,
File "/home/lamberk/python/picasel/rc_cross_upsell/venv/lib/python3.5
/site-packages/django/http/response.py", line 520, in __init__
data = json.dumps(data, cls=encoder, **json_dumps_params)
File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
return _iterencode(o, 0)
File "/home/lamberk/python/picasel/rc_cross_upsell/venv/lib/python3.5
/site-packages/django/core/serializers/json.py", line 118, in default
return super(DjangoJSONEncoder, self).default(o)
File "/usr/lib/python3.5/json/encoder.py", line 179, in default
raise TypeError(repr(o) + " is not JSON serializable")
}}}

Is it a bug or i can't understand something?

--
Ticket URL: <https://code.djangoproject.com/ticket/27492>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 16, 2016, 6:22:47 AM11/16/16
to django-...@googlegroups.com
#27492: JsonResponse can't handle model_to_dict() with empty ImageField
-------------------------------------+-------------------------------------
Reporter: Komarov Valentin | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 1.10
Severity: Normal | Resolution:
Keywords: jsonresponse | Triage Stage:
model_to_dict imagefield | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/27492#comment:1>

Django

unread,
Nov 16, 2016, 7:09:17 AM11/16/16
to django-...@googlegroups.com
#27492: JsonResponse can't handle model_to_dict() with empty ImageField
-------------------------------------+-------------------------------------
Reporter: Komarov Valentin | Owner: nobody
Type: Bug | Status: closed

Component: HTTP handling | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: jsonresponse | Triage Stage:
model_to_dict imagefield | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

The docstring for `model_to_dict()` says, "Returns a dict containing the
data in `instance` suitable for passing as a Form's `initial` keyword
argument." As far as I see, it's not meant for serializing a model for
`JsonResponse`.

--
Ticket URL: <https://code.djangoproject.com/ticket/27492#comment:2>

Django

unread,
Nov 16, 2016, 7:24:28 AM11/16/16
to django-...@googlegroups.com
#27492: JsonResponse can't handle model_to_dict() with empty ImageField
-------------------------------------+-------------------------------------
Reporter: Komarov Valentin | Owner: nobody
Type: Bug | Status: closed
Component: HTTP handling | Version: 1.10
Severity: Normal | Resolution: invalid
Keywords: jsonresponse | Triage Stage:
model_to_dict imagefield | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Komarov Valentin):

Where is a right way to serialize empty ImageField?

--
Ticket URL: <https://code.djangoproject.com/ticket/27492#comment:3>

Django

unread,
Nov 16, 2016, 7:32:06 AM11/16/16
to django-...@googlegroups.com
#27492: JsonResponse can't handle model_to_dict() with empty ImageField
-------------------------------------+-------------------------------------
Reporter: Komarov Valentin | Owner: nobody
Type: Bug | Status: closed
Component: HTTP handling | Version: 1.10
Severity: Normal | Resolution: invalid
Keywords: jsonresponse | Triage Stage:
model_to_dict imagefield | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

Please use [wiki:TicketClosingReasons/UseSupportChannels our support
channels] for usage questions. Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/27492#comment:4>

Reply all
Reply to author
Forward
0 new messages