2

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
  • 2
    Yes that is possible, but a lot of how Django can help is then not available. In that case, Django will look more (in terms of what it can do) like Flask. The idea of Django models is not only to populate and query the database, but also construct ModelForms, Serializers, construct queries in ListViews, DetailViews, etc. to make webdevelopment more convenient. Commented Nov 2, 2020 at 21:28

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer! My biggest worrie is that I will not be able to create a user panel and functionalities like register and login, because on all video tutorials that I saw people use Models and functions related to them so it's hard for me to imagine how to implement this part of a project.
@dwastoliki: well all ORM calls can be replaced with raw queries, since behind the curtains eventually it makes queries to the database. But the idea of Django is assist users with that. Not only because it is more convenient, but also from a security point of view. For example Django's User model has logic to automatically hash the password when using User.objects.create_user(...), if you decide to do this yourself, you need to take care of all these things yourself.
Raw SQLqueries might be not a big problem but creating a user panel and allow user to make orders oh his account is a huge challenge for me. Maybe even too huge as a beginner in django, I think I will skip this part of a project.
@dwastoliki: but I don't see what Django has to do with that. Imagine that you use another technology like Flask, then you face the same challenges: you will still need to return HTTP responses with HTML, and make updates to the database accordingly. Django will not cause trouble with this, but as said, it will be very similar to how you would implement this with a more lightweight framework like Flask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.