【问题标题】:How to return single object as a json response inside django function?如何在 django 函数中将单个对象作为 json 响应返回?
【发布时间】:2021-07-28 05:38:15
【问题描述】:

我只想将 User 模型的单个对象作为 json 响应。我不知道我该怎么做..

现在我收到“用户”对象不可迭代的错误

这是我的功能:

def some_view(username_to_toggle):
    print(username_to_toggle,"User to togle")
    user = User.objects.get(username__iexact=username_to_toggle)
    print(user,"User object")
    user_json = serializers.serialize('json', user,many=False)
    print(user,"User json")
    return HttpResponse(user_json, content_type='application/json')

追溯:

Traceback (most recent call last):
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/contrib/auth/mixins.py", line 52, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/profiles/views.py", line 65, in post
    profile_, is_following,json_follower = UserProfile.objects.toggle_follow(request.user, request.user.id ,username_to_toggle,json_follower)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/profiles/models.py", line 72, in toggle_follow
    json_follower = some_view(username_to_toggle)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/profiles/models.py", line 45, in some_view
    user_json = serializers.serialize('json', user,many=False)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/serializers/__init__.py", line 128, in serialize
    s.serialize(queryset, **options)
  File "/home/hamza/Mydjangoproject/grabpublic/grabpublic/Grab_env/lib/python3.6/site-packages/django/core/serializers/base.py", line 90, in serialize
    for count, obj in enumerate(queryset, start=1):
TypeError: 'User' object is not iterable

【问题讨论】:

  • 你能发布完整的回溯吗?
  • 当然,我用 trackbacl 更新了我的问题

标签: json django django-models django-rest-framework django-views


【解决方案1】:

来自django.core.serializersserialize 方法没有many 选项,它总是需要一个模型对象的列表或查询集来进行序列化。如果您总是只想序列化一个对象,请尝试使用其中任何一个:

第一种方法,使用查询集:

def some_view(username_to_toggle):
    print(username_to_toggle,"User to togle")
    users = User.objects.filter(username__iexact=username_to_toggle)
    print(users,"User objects")
    user_json = serializers.serialize('json', users)
    print(user,"User json")
    return HttpResponse(user_json, content_type='application/json')

或第二种方法,使用列表:

def some_view(username_to_toggle):
    print(username_to_toggle,"User to togle")
    user = User.objects.get(username__iexact=username_to_toggle)
    print(user,"User object")
    user_json = serializers.serialize('json', [user])
    print(user,"User json")
    return HttpResponse(user_json, content_type='application/json')

many=Falsemany=True 是 Django REST Framework 序列化器的一个特性,但它们的用法不同且更复杂,因为您需要为要序列化的每个模型声明一个单独的序列化器。

【讨论】:

  • 首先,非常感谢您的回答,非常感谢:) ...顺便说一句,我应用了您在答案中列出的两种方法。我遇到了一个例外,TypeError: Object of type 'HttpResponse' is not JSON serializable
  • 所以这完全意味着序列化方法必须占用一个列表作为第二个参数??
猜你喜欢
  • 2023-03-14
  • 2020-03-06
  • 2017-12-04
  • 2014-10-07
  • 1970-01-01
  • 2019-06-30
  • 2014-06-02
  • 2016-12-07
  • 1970-01-01
相关资源
最近更新 更多