Wednesday, June 28, 2017

Re: django JsonResponse

You may want to take a look at http://www.django-rest-framework.org/
The error tells you exactly what's wrong: MyModel object is a Model, which is not iterable. You want to serialize the MyModel object, which is possible with Django Rest Framework.
I usually advise against using JsonResponse over DRF. You should only use it if you're using it once or twice in your app and it's not related to a model or something that's complex.

On Jun 28, 2017 4:12 PM, shahab emami <royaramezan@gmail.com> wrote:
hello

i want to use ajax in my django project.

i have no problem with writing ajax method or url. my problem is in my django view.

i send a pk from template with ajax and in my view:

def edit(request):
   
pk = request.GET.get('pk' or None)
   
    instance
= models.MyModel.objects.get(pk=pk)

   
return JsonResponse(instance)

can anybody tell me if this is a good way to work with JsonResponse?

when i write this i get this error:
MyModel object is not iterable

what i have to do to solve this error.

every search that i make is about SimpleJson but i now SimpleJson is before django 1.7

i am working with django 1.11

can anybody save me from this

thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4efc338d-7326-4c98-9e03-c88ba0834ff3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5953b1ec.ce0e1c0a.136e9.792cSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment