A couple of weeks ago I had this task in Agileful to refactor and improve some AWS lambda functions.
There was a bunch of pure SQL queries written alongside python codes which made me think, what if we could use standalone Django ORM in our python lambda functions?
So I did some digging on how we can use my beloved ORM in python scripts despite the serverless stuff, there were some vague and old answers but I managed to make something out of it. It’s as simple as defining a database connection settings and calling django.setup().
Standalone Django ORM
Here is the minimal python code needed to use standalone Django ORM:
Plus these two files you need to create your desired Django app models. (e.g. videos_app/models.py)
There is one downside in this way of using Django and that is the lack of manage.py ! I haven’t got time to try adding manage.py commands so I added some helper scripts like the ones below: