2

This might sound like a bit of an odd question - but is it possible to load data from a (in this case MySQL) table to be used in Django without the need for a model to be present?

I realise this isn't really the Django way, but given my current scenario, I don't really know how better to solve the problem.

I'm working on a site, which for one aspect makes use of a table of data which has been bought from a third party. The columns of interest are liklely to remain stable, however the structure of the table could change with subsequent updates to the data set. The table is also massive (in terms of columns) - so I'm not keen on typing out each field in the model one-by-one. I'd also like to leave the table intact - so coming up with a model which represents the set of columns I am interested in is not really an ideal solution.

Ideally, I want to have this table in a database somewhere (possibly separate to the main site database) and access its contents directly using SQL.

djbp
  • 714
  • 8
  • 24

3 Answers3

4

You can always execute raw SQL directly against the database: see the docs.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks - that was the solution that I was expecting to go for. I was just trying to see whether there was another way. Since posting this question, I have come across [another post](http://stackoverflow.com/questions/1545714/reverse-engineer-mysql-database-to-create-django-app) on SO which I think may help me achieve what I am looking to do. – djbp Jun 26 '14 at 18:57
1

There is one feature called inspectdb in Django. for legacy databases like MySQL , it creates models automatically by inspecting your db tables. it stored in our app files as models.py. so we don't need to type all column manually.But read the documentation carefully before creating the models because it may affect the DB data ...i hope this will be useful for you.

uthay
  • 123
  • 2
  • 11
  • That's the solution I referenced in my comment on the accepted answer. Thanks for the suggestion though. – djbp Jun 05 '17 at 11:09
0

I guess you can use any SQL library available for Python. For example : http://www.sqlalchemy.org/

You have just then to connect to your database, perform your request and use the datas at your will. I think you can't use Django without their model system, but nothing prevents you from using another library for this in parallel.

Raphael Laurent
  • 1,941
  • 1
  • 17
  • 27