Ahmed Yasin
Ahmed Yasin

Reputation: 948

How to return single object as a json response inside django function?

I just want to get a single object of User model as a json response. I couldn't figure out how can i do that..

right now i'm getting an error that 'User' object is not iterable

Here is my function:

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 :

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

Upvotes: 1

Views: 1068

Answers (1)

GwynBleidD
GwynBleidD

Reputation: 20549

The serialize method from django.core.serializers doesn't have many option and it always needs a list or a queryset of model objects for the serialization. If you always want to serialize only one object, try using either of those:

first method, using queryset:

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')

or a second method, using list:

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=False or many=True is a feature of Django REST Framework serializators, but their usage is different and more complex as you need to declare a separate serializer for every Model you want to serialize.

Upvotes: 2

Related Questions