in models.py you can define a 'as_json' function

def as_json(self):
    return dict(
        identification_number = self.id
        awesome_picture = self.picture
    )

in views.py:

class My_View(View):
    def get(self, request):
        if request.is_ajax():
            q = request.GET.get('q', '')
            answer = model.objects.get(pk=q)
            if not answer:
                this_answer = dict()
                this_answer['results'] = 'did not find anything'
                return JsonResponse(this_answer)
            this_answer = [d.as_json() for d in answer]
            return HttpResponse(json.dumps(this_answer,
content_type='application/json')
        else:
            this_answer = dict()
            this_answer['not_allowed'] = 'you are not allowed here.'
            return HttpResponse(this_answer,
content_type='application/json')

hope this helps!

Den tis 9 apr. 2019 kl 13:30 skrev sairanganadh Narayana <
ssaaii4...@gmail.com>:

> I'm trying to get run model.objects.filter(id=2) from javascript and show
> on database. I am unable to get solution for this. Please guide me solution
> for this or alternative for this.
>
> --
> 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+unsubscr...@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/569ce414-b717-4348-a688-b92904036864%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/569ce414-b717-4348-a688-b92904036864%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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+unsubscr...@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/CAPLZMbMaB%3Dus-q3X%2BHQJNuQyL-74MdjpPUDJ1Mt8wOCcPrH-_g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to