Logo

dev-resources.site

for different kinds of informations.

Using Google Cloud Firestore with Django's ORM

Published at
5/20/2024
Categories
django
webdev
googlecloud
firestore
Author
kazade
Author
6 person written this
kazade
open
Using Google Cloud Firestore with Django's ORM

Django has long been the most popular Python framework for developing web applications. One of its most powerful features is its built in object-relational mapper (ORM) which is designed to flexibly and safely interact with SQL databases in an abstract way.

A long time ago, a fork of Django called “Django-nonrel” experimented with the idea of using Django’s ORM with a non-relational database; what was then called the App Engine Datastore, but is now known as Google Cloud Datastore (or technically, Google Cloud Firestore in Datastore Mode). Since then a more recent project called "django-gcloud-connectors" has been developed by Potato to allow seamless ORM integration with Google Cloud Datastore.

The intention was always to expand that functionality to other Google hosted no-SQL databases, and today I’m pleased to announce that django-gcloud-connectors now has support for Google Cloud Firestore in Native Mode. This means you can now build a robust Django-powered website or API driven from a Firestore database which could for example be shared with your Firebase backed mobile application.

It might seem that using Django’s relational mapper on a non-relational database would be like forcing a square peg into a round hole - but it turns out that the Django ORM works quite elegantly with a non-relational database, and gives you the tools you need to build reusable patterns to get the most out of a massively scalable database.

Take for example the schemaless nature of a non-rel database like Cloud Datastore which allows each document to have different properties to the other documents in the same table; while powerful, in practice you generally need a schema to write code against and to ensure that your data remains consistent. Django’s model structure allows you to maintain a schema (with validation) that can change over time. You can leverage model signals, field defaults or request middleware to migrate data on the fly, without the need for running potentially slow or disruptive database migrations. You hardly need to worry about the database at all while developing, simply add a new model or field in your code and use it. Django with a non-relational database is great for rapid prototyping.

There are limitations of course; non-relational databases don’t do complex queries - you can’t perform joins; you can have foreign keys, but you can’t fetch or order across those relations in one database query; you can have many-to-many relationships, but not with Django’s ManyToManyField (you need to use a custom list field instead). You might just find some queries are impossible.

These limitations mean you have to solve problems in a slightly different way. You’ll find you’ll design your models around the queries you need to make; you’ll sometimes have to pre-calculate things when you save a document to make it possible to quickly query for it later, or perhaps use other services like Big Query or background task processing.

But you can almost always assume that once your code works, it’ll scale well no matter how much data you throw at it; there’s no risk of running an unindexed query on a million rows, or having to think about database replication or sharding. You can rapidly build a product for a startup and know that things will just ramp up when it hits the big time.

I highly recommend you give it a try, simply follow the instructions in the django-gcloud-connectors repository and you’ll be up and running in no time. If you’re hosting on Google App Engine you might also be interested in the Djangae project which provides useful utilities and Django apps to integrate with Google Cloud services.

firestore Article's
30 articles in total
Favicon
Dev Video Review: Firestore Data Structure, Limitations, and IMHO
Favicon
Do you need a No Code tool for Firebase?
Favicon
Firebase: The Ultimate Backend for Your CMS
Favicon
NgSysV2-10.1: Firestore CRUD templates
Favicon
NgSysV2-3.3: A Serious Svelte InfoSys: Firebase D/b rules and Login
Favicon
NgSysV2-3.4: A Serious Svelte InfoSys: Rules-friendly version
Favicon
NgSysV2-3.5: A Serious Svelte InfoSys: Client-Server Version
Favicon
Dive into the world of serverless - GCP Edition
Favicon
Visualizing Firebase Data: Unlocking the Power of Real-Time Insights
Favicon
Implementing Batch Write Operations in Firestore with Express
Favicon
Enforcing Firebase App Check for Firestore with Initialization Configuration
Favicon
Retrieving User Roles from Firestore in a Next.js Application
Favicon
How to Keep Your Custom Claims in Sync with Roles Stored in Firestore
Favicon
Scheduling Events in Firebase Firestore with Server Timestamps
Favicon
Using Google Cloud Firestore with Django's ORM
Favicon
Firebase Realtime Database vs Cloud Firestore
Favicon
Real-Time Data Handling with Firestore: Tracking Pending Orders
Favicon
How to programmatically backup your Firestore database with simple steps
Favicon
Understanding Real-Time Data with Firebase Firestore in JavaScript
Favicon
Enabling Offline Capabilities in Firebase with IndexedDB Persistence
Favicon
Querying Firestore for Capital Cities with JavaScript
Favicon
Explorando o Firebase: Uma Plataforma Poderosa para Desenvolvimento de Aplicativos
Favicon
Using Firestore in Apps Script
Favicon
What is Flutter, how do I get started ??!!!
Favicon
How to Stream Data From Firebase to BigQuery Easily
Favicon
Retrieve a list of data under specific user collection in flitter
Favicon
Tentando não ficar pobre antes de ficar rico criando uma Startup de serviços de inteligência artificial
Favicon
Trying to Maintain a Workable Budget Creating a Chatbot Using GPT and Vector Database
Favicon
Uptime Monitoring with Firebase
Favicon
Firestore data modeling

Featured ones: