Using django without models

Viewed 922

Is it possible to use Django but without models? Instead of models I want to use raw sql queries with help of mysql cursor.

I want to implement Car Rental site to rent a car. There will be three tables: Cars, Users, Orders. Anyone can sign up on the webpage, sign in and make orders.

The only limitation is that a have to do this using raw sql queries so I think that I have to avoid using models.

Is this even possible?

I'm new to Django and this might be dump question but need to know cause this is my academic project.

1 Answers

Is this even possible?

Yes that is possible, but a lot of ways how Django can help with webdevelopment are based on its models. For example based on a model Django can make a ModelForm [Django-doc] to automate rendering HTML forms that map to the model, validating user input, and saving it to the database. You can still use a simple Form [Django-doc], but then you need to implement the validation, etc. yourself.

Class-based views like a ListView [Django-doc], DetailView [Django-doc], CreateView [Django-doc], etc. can automate a lot of the logic based on a model. These can then automatically query for the records of the model, or construct a form to create a new record. Often customizing such view to is not much effort. If you however do not define such models, you will need to implement a lot of the logic yourself.

The Django documentation has a section about executing custom SQL queries directly that explains how to use the connection configured in the settings.py to perform raw queries together with tips to avoid SQL injection.

That being said, without models, Django probably can offer approximately only as much help as Flask, so in that case there is likely not a clear advantage to use Django over Flask, especially since for small applications, a Flask application is very minimal.

Related