13

I have been crawling around its doc but mostly it uses database with model.

The problem is my database is too large and I don't want to create any models

since it's legacy one, and I will have to call different tables dynamically,

so I just want to pull data from it. Is that possible in django?

4
  • It's not clear why you don't want to create models. They don't create extra load and are by far the easiest way to interact with the db. Commented Nov 29, 2016 at 19:02
  • 1
    @DanielRoseman I have like 500+ tables in the database. And originally it's using something like table sharding technique. So the table name varies per query(like user_table_0001, user_table_0002, etc). So i think it's not good for creating model since the name of table always changes, is it? Commented Nov 29, 2016 at 19:09
  • 2
    Did you find a solution to your problem ? If yes, what did you do ? Commented Jan 1, 2018 at 7:13
  • The two possible options are already described below. There is nothing else that can be done. You just can't use the Django ORM without defining a Django model. See this related question as well. Commented Sep 11, 2023 at 22:28

3 Answers 3

8

You can go around the model layer and use sql directly. However, you will have to process the tables in python, not having the advantage of using ORM objects.

https://docs.djangoproject.com/en/1.10/topics/db/sql/#executing-custom-sql-directly

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

Comments

3

As pointed out in a comment, Django provides a way to automatically generate the models from the legacy database with inspectdb. This guide describes the few manual steps required to "clean" the automatically generated models.

While this doesn't directly answer the stated question of avoiding models, it does address your issue of not wanting to create them yourself, due to the large database.

Comments

-2

Data should be stored somewhere. There are a lot of ways to store data, but the most reliable one is a database (hence the name).

You could be storing data in a JSON file and save that. You could also be storing data in environment variables. You can even store data in a plain text file. All of those are NOT recommended. I would just try to use a database, any type of database (MongoDB / Postgres / MySQL, anything). That's what it is meant for.

2 Comments

Let me make my point clear. I have a legacy database already and I only need to connect to them(which I can now). But my problem is, I want to get data from it without creating a 'model' for any table.
Sorry, misunderstood your question. I don't think there is a way to use Django ORM without creating the models. You can auto create them: docs.djangoproject.com/en/1.10/howto/legacy-databases . One other thing that might work, would be to create one model and then use raw sql queries (you can pick a table with raw queries). This is a lot more work, but that should be doable.

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.