Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. Rewrite the model so it's shared by both Django and my program
  2. Write a layer that interfaces between Django and my Program
  3. 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?

like image 860
Lionel Avatar asked Apr 09 '26 11:04

Lionel


1 Answers

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.

like image 164
Kenny Shen Avatar answered Apr 12 '26 10:04

Kenny Shen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!