using Django on an existing mysql schema

73 views
Skip to first unread message

William Muriithi

unread,
Jan 30, 2015, 10:19:25 AM1/30/15
to django...@googlegroups.com
‎Hello,

I am new to Django and planning to use it for a project I have in mind. I am strong in mysql, but not too good in developing. In fact, the secondary purpose of this project is to improve my python development skills.

With such a background, I went about doing my data modeling and then set up views on top to de normalize the underlying schema and pushing most of the business logic in mysql. I want to use Django mainly to insert and display, which increases the chance of success with few bugs.

Problem is, with Django, it seems things are done the other way around. After playing with it, I noticed it changes the keys from unsigned to signed, something I feal is bad from database point of view. It converted enum to char, which to me is less appealing. In short, I don't like the change its introducing. ‎

My question is, can one use Django with an existing database without getting in trouble with the framework? I believe this has to be possible somehow as most companies work from data backwards. Would appreciate a good advice on how you guys and girls went‎ about using Django from existing schema. Or a Django book that take that perspective. The once I have read advice me to leave database work to orm.

Regards,

William


Tobias Dacoir

unread,
Jan 30, 2015, 2:14:04 PM1/30/15
to django...@googlegroups.com
I'm certainly no Django expert, but I'm not sure if you can use the orm and an already existing database. However the documentation states that you can write all queries yourself, essentially not using the orm. However if you do that, I'm wondering why you want to use Django at all?
And pushing business logic into the database sounds like bad design to me (model and controller should be decoupled in MVC like patterns).

About the signed and unsigned changes - did you check the documentation: https://docs.djangoproject.com/en/1.7/ref/models/fields/
And enum is (afaik) not available in Python, maybe that's why it changed it to char.

Russell Keith-Magee

unread,
Jan 30, 2015, 7:11:21 PM1/30/15
to Django Users
On Sat, Jan 31, 2015 at 3:14 AM, Tobias Dacoir <fal...@gmail.com> wrote:
I'm certainly no Django expert, but I'm not sure if you can use the orm and an already existing database.

Yes, you absolutely can. There's even a management command (inspectdb) to help write the wrapper models, and a Meta flag (managed=False) to make sure the migration tools won't touch those models.
 
However the documentation states that you can write all queries yourself, essentially not using the orm. However if you do that, I'm wondering why you want to use Django at all?

Because Django has a lot more than just an ORM. There's a forms layer, authentication tools, and lots more.
 
And pushing business logic into the database sounds like bad design to me (model and controller should be decoupled in MVC like patterns).
 
It depends what logic you're talking about, and what other tools will be using the same database. "MVC" separation like you are describing assumes that the Controller is shared; if you have to re-implement the controller in two languages, then there's a very strong argument to push some of the controller's functionality into the database.

Yours,
Russ Magee %-) 

Russell Keith-Magee

unread,
Jan 30, 2015, 7:18:59 PM1/30/15
to Django Users
Yes, you can. Django's ORM is there to make your life easier, but there's no requirement that you use it to create your tables, or even to execute queries. There's an inspectdb management command to help build wrapper models for your database; but if you want, you can avoid the ORM entirely, and use raw database cursors to talk directly to the database. 

Of course, if you do this, you're going to miss out on at least some of the power of Django. 

For example, Django has a forms library, which is quite powerful. You can use it without using Django's ORM. However, if you *are* using Django's ORM, you can use ModelForms, which is a way to automatically generate a form for a model.
 
You're also going to miss out on the community of apps that are out there to help you get functionality running easily. For example, if you don't have Django ORM models, then Django's admin interface wont work. A lot of the power of Django is that it has a good "meta" understanding of the data in the database; if you take that away, then the functionality that relies on it obviously wont work.

There's nothing inherently wrong with this - you can code everything yourself if you want and treat Django as little more than a request routing library. The only problems you're going to experience are related to the fact that documentation out there will generally assume that you're using Django models, not rolling your own. My only concern is that this might leave you with an odd perception of the capabilities of Django.

Yours,
Russ Magee %-)


Reply all
Reply to author
Forward
0 new messages