A
A
Alexander2017-07-03 10:17:39
Django
Alexander, 2017-07-03 10:17:39

How to properly make json response dict?

work = Work.objects.order_by('-id').first()
    work = serializers.serialize('json', dict(
        work=work,
        work_images=work.workimage_set.all
    ))
    return JsonResponse(work)

Mistake:
response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/path_project/views.py", line 37, in get_yet_work
    work_images=work.workimage_set.all
  File "/path_project/env/lib/python3.6/site-packages/django/core/serializers/__init__.py", line 129, in serialize
    s.serialize(queryset, **options)
  File "/path_project/env/lib/python3.6/site-packages/django/core/serializers/base.py", line 83, in serialize
    concrete_model = obj._meta.concrete_model
AttributeError: 'str' object has no attribute '_meta'
[03/Jul/2017 07:13:15] "POST /services/mobile/get_yet_work/ HTTP/1.1" 500 13972

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-07-03
@kentuck1213

First, what do you have serializers.serialize. Where does this method or function come from, and what does it do?
Second, workimage_set.all is a method. I suspect you wanted to call it, for this you need to write parentheses at the end. Otherwise, the construction does not make sense - this is an attempt to serialize the method, and not the result of its work.
Thirdly, it's probably time for you to switch to using the Django Rest Framework. This is more correct and more convenient than serializing everything manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question