1

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
2
  • Can you post the full traceback? Commented May 5, 2021 at 21:44
  • Sure, i updated my question with trackbacl Commented May 5, 2021 at 21:46

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

First of all , I really appreciate that you answered, Thank you so much :) ... by the way i applied both methods you listed in your answer. And i'm getting an exception that TypeError: Object of type 'HttpResponse' is not JSON serializable
So this totally implies that serialize method must take up a list as the second param??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.