Create models using raw SQL queries

33 views
Skip to first unread message

cameron hochbrg

unread,
Mar 26, 2019, 4:46:49 PM3/26/19
to Django users
hello,

for a project for a database class, we wanted to create a web app using django. However, our teacher wants us to use raw SQL queries for everything involving the database. As such I was wondering if it was possible to create mdoels using raw SQL queries.

thank you in advance

PASCUAL Eric

unread,
Mar 27, 2019, 6:16:54 PM3/27/19
to django...@googlegroups.com
Hi,

Not sure Django is the right choice in your case since one of its main purposes is to hide the SQL stuff by putting the ORM in front. 

You'd better use Flask, Tornado or any other Web framework which does not come with any special feature WRT data access.

Best

Eric


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of cameron hochbrg <caos...@gmail.com>
Sent: Tuesday, March 26, 2019 21:36
To: Django users
Subject: Create models using raw SQL queries
 
hello,

for a project for a database class, we wanted to create a web app using django. However, our teacher wants us to use raw SQL queries for everything involving the database. As such I was wondering if it was possible to create mdoels using raw SQL queries.

thank you in advance

--
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/8145efe7-c169-43d6-86e6-e0d64415a764%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon A

unread,
Mar 27, 2019, 9:33:21 PM3/27/19
to Django users
hi Cameron, as Eric said, by doing this you might not fully take advantage of the features of django. But if you still want to proceed maybe you can use the managed option for models. https://docs.djangoproject.com/en/2.1/ref/models/options/#managed.

with this approach, you'll create the database tables manually first OUTSIDE of django, then connect django to it afterwards. I haven't tried it before but this might be what you need.

also if your teacher requires to perform raw sql queries rather than use the ORM, there is an option to use raw sql queries. https://docs.djangoproject.com/en/2.1/topics/db/sql/#executing-custom-sql-directly


On Thursday, March 28, 2019 at 6:16:54 AM UTC+8, Eric Pascual wrote:
Hi,

Not sure Django is the right choice in your case since one of its main purposes is to hide the SQL stuff by putting the ORM in front. 

You'd better use Flask, Tornado or any other Web framework which does not come with any special feature WRT data access.

Best

Eric


From: django...@googlegroups.com <django...@googlegroups.com> on behalf of cameron hochbrg <caos...@gmail.com>
Sent: Tuesday, March 26, 2019 21:36
To: Django users
Subject: Create models using raw SQL queries
 
hello,

for a project for a database class, we wanted to create a web app using django. However, our teacher wants us to use raw SQL queries for everything involving the database. As such I was wondering if it was possible to create mdoels using raw SQL queries.

thank you in advance

--
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...@googlegroups.com.
To post to this group, send email to djang...@googlegroups.com.

cameron hochbrg

unread,
Mar 27, 2019, 11:46:43 PM3/27/19
to django...@googlegroups.com
Thank you for your response. I am aware that django has for purpose to hide the SQL. the reason why I wanted to use django is that I had used it before and I didn't want to have to learn a new framework if possible.

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.

PASCUAL Eric

unread,
Mar 28, 2019, 4:15:10 AM3/28/19
to django...@googlegroups.com
Hi Cameron,
the reason why I wanted to use django is that I had used it before and I didn't want to have to learn a new framework if possible.
Makes sense.

However, light FWs such as Tornado (which I like a lot, and prefers over Flask BTW)  have a really small learning curve for a basic usage (i.e. serving dynamic pages with the help of templates). If you're already familiar with Django, you'll be productive with Tornado is less than one hour, if not shorter.

If you have the option to invest a bit of time, it deserves a look because you'll get the ROI of having one more weapon in your arsenal. You'll thus be able to select the best suited option instead of relying on the same one every time. Although Django is really great (I work a lot with it for years for my job as well as for private projects, and I really enjoy it), there are situations it's not the optimal choice. For instance, it can be a bit overkill when you don't have to play with a data model justifying an ORM and such.

One important word about Tornado, and why I like it so much : it is based on the asynchronous paradigm and despite being single thread and single process (although it supports a multi-process execution mode for taking advantage of multi-cores CPUs) it is able to serve a lot of requests thanks to this. This is especially true when these requests are IO bound (e.g. DB access, network requests...). Knowing such an architecture and knowing how to properly implement request handlers to leverage its power is IMHO a valuable experience. Tornado is amazing when your target is an embedded system with constrained resources, such as a Raspberry Pi or alike. I've developed several real world apps in this context and it plays amazingly well. 

Even if Tornado has not as many batteries included as Django (it brings nothing for data access f.i.) it comes with a lot of valuable features inside in addition to templating and request serving (CSRF, CORS, sessions, user authentication, asynchronous network clients, Web sockets, I18N support, UI modules,...). In addition, you don't need to put a WSGI server in front, since it includes its own asynchronous production grade server (not a dev server such as Django's runserver but a real production one).

Maybe all this looks a bit like Tornado propaganda or Django bashing. It's neither of these of course, but only my own experience.

Best regards

Eric


Sent: Thursday, March 28, 2019 04:46
To: django...@googlegroups.com
Subject: Re: Create models using raw SQL queries
 

John Crosby

unread,
Mar 28, 2019, 7:34:39 AM3/28/19
to django...@googlegroups.com
Cs50 from Harvard do quite a nice YouTube course on this. They start with SQL.queries direct to database and move into flask and.Django. It might be worth a look: https://youtu.be/Eda-NmcE5mQ
Reply all
Reply to author
Forward
0 new messages