How to run ORM commands from DOM Javascript?

28 views
Skip to first unread message

sairanganadh Narayana

unread,
Apr 9, 2019, 7:30:15 AM4/9/19
to Django users
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.

Robin Riis

unread,
Apr 9, 2019, 8:21:24 AM4/9/19
to django...@googlegroups.com
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 <ssaai...@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...@googlegroups.com.
To post to this group, send email to django...@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.
For more options, visit https://groups.google.com/d/optout.

sairanganadh Narayana

unread,
Apr 15, 2019, 4:23:10 AM4/15/19
to Django users
Thanks for your response. But, I need to run ORM(model.objects.filter(pk=1)) commands in javascript and show results in HTML pages. I was wrong in question. Please suggest any possibility.


To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Sithembewena L. Dube

unread,
Apr 15, 2019, 4:27:43 AM4/15/19
to django...@googlegroups.com
You cannot run Django's Python code in JavaScript in a browser. The advice you have been given applies.

Kind regards,
Sithu


Sent with Shift

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages