Python Backend Logic Adding MVC Framework (Django)
I have a Python CLI program, with SQL database models and want to add a frontend with a MVC framework (such as Django). What's the best way to interface my program with the framework, if I've already defined my models?
Do I either:
- Rewrite the model so it's shared by开发者_如何学JAVA both Django and my program
- Write a layer that interfaces between Django and my Program
- Remove the model from My Program and let Django handle it
Choice #1: Shared Model
My Program / | \ Binaries Model Classes | Django / \ View Controller
Choice #2: Create a Bridging Library
My Program / | \ Binaries Model Classes | My-Bridge | Django / | \ View Model Controller
Choice #3: Use Django for Most Work and Remove Model from My Program
Classes \ My Program / | Binaries | | My-Bridge | Django / | \ View Model Controller
I'm avoiding Choice #1 (Create a Shared Model) because I don't know how to create a shared model using Django's ORM and SQLAlchemy.
I'm unsure about Choice #2 (Creating a Bridge) because I don't know if this uses Django to its full extent. From the documentation, it seems as thought Django should handle the Model, given that its a MVC framework.
I'm also avoiding Choice #3 (Removing the Model from Program) because I would have to re-write all the SQLAlchemy ORM logic that uses the SQLAlchemy model in My-Program.
What do you guys think? Which choice is best given that I've already written the CLI version of my program?
I love Django but given this scenario, you could also take a look at Pylons too, since they support SQLAlchemy. Or you can still work with SQLAlchemy by importing it into your views. See this post on an example for doing that.
Let's start by making observation that frontend which modifies data bypassing backend doesn't sound like a good design. Having said this I don't see any technical reason it can't be done. We have to remember that it's database that keeps integrity of data. That's why you should be able to use different ORMs or one ORM with different models with the same database.
The model ORM you use definitely dictates how the integration between backend and frontend should be played out.
I wouldn't say it dictates the choice. It would be simpler to have the same ORM for both backend and frontend but that's not a must.
I'd try to migrate much of the logic of 'My Program' to a module that is importable. Then, have django import this and share the settings for the database across them. It'd also be possible to have the django instance running, and doing the grunt work, and let the 'My Program' make remote calls to it. Granted that would probably take the most work.
Rewriting the models to conform to django's api sounds like the least amount of work, and the most 'correct' way to solve your problem. Unless you're doing some 'advanced' database access, Djangos ORM should be able to handle what you want to do cleanly. Using the full stack also pays dividends later when you decide you want forms wrapping your models, and all the various other bits of the framework that expect a model structure.
精彩评论