How To: Setup your root App folder for the Cloud

How To: Setup your root App folder for the Cloud

Solving the headache of nested folders when deploying to the cloud

This is a quick note on how to mix and match the ORM from Django into an application Flash or Tornado, consider the following folder structure:

mysite/
    app.py
    server/
        manage.py
        my_orm/
            __init__.py
            admin.py
            apps.py
            models.py
            test.py
            view.py    
        mysite/
            __init__.py
            settings.py
            urls.py
            asgi.py
            wsgi.py

The structure provided has a Flask or Tornado application on the app.py file. However, the previous developer did not know how to use SQL and so decided to use an Object Relational Model (ORM) and decided on the one provided by Django. The web application runs well, the database access works with the ORM as expected and everything is great. Now, the developer leaves, you inherit the code base and manage to run it locally and it works ok. You might not love the ORM, but it is easy enough that the junior programmer can contribute to the code without having to understand SQL.

Lots of developers do not know how to use SQL, if you don't know you should learn

The application performs and runs well in the internal VMs. However, the project needs to be on the cloud as the company is moving now towards cloud-only deployments as it is the new company mission to reduce cost. The code base is deployed to your providers, we are using MS Azure, the local scripts are run to update the data model as you have switched the ORM settings to using environment variables so you can connect to your cloud database and everything seems to be going smooth. Now we push the server code to an App Service, and BOOM, everything is NOT peachy! The structure of your code is having issues finding the correct folders for the ORM, how do we fix this? We need to add to the python path the location of the Django section of the code and we do that as follows:

The piece code above we set the location of our Django settings to our Python path, then we call the django.setup() method setting up our ORM properly. This has to happen before your main application launcher. After it, everything with the ORM is working again.

This little piece of code can save you a lot of headache

Did you find this article valuable?

Support Failing 2 Build by becoming a sponsor. Any amount is appreciated!