简体   繁体   中英

Django Transforming to Raw Query

I am working on a django project and I must not use django's get method for getting data from datbase. I could not understand how can I make this query in sqlite.

obj = get_object_or_404(MyModel, pk=1)

In general where can I learn this queries SQL equivalents?

Normally, Querysets have a query attribute containing the query to be executed.You can use like that:

print(YourModel.objects.filter(field=field_value).query)

if you implement get_object_or_404 to raw sql query, you can use select command and check sql results count.If there is no result, you can raise 404 exception:

your_obj = YourModel.objects.raw('SELECT * FROM your_model where pk= %s',[your_pk]):
if not your_obj:
    raise Http404

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM