Skip to main content

NoSQL object model database for django ORM integration

Project description

Build Status Build status Documentation Status License: GPLv3 Organization Views PRs Welcome GitHub pull-requests Language

django-firebase-orm.

Django like models for NoSQL database Firestore Integrating with django's ORM. This is a fork and improvement of firebase_orm project which was initially forked from xavx0z and stopped maintaining it.

I made a few changes and improvements to suit my liking:

  1. Changed the need of creating a second settings.py file in the root of your django project to now only require that you define the neccessary configurations in your project's settings module. Note that django is now an explicit dependency
  2. Created a new package for this app on pypi under django-firebase-orm

It is my desire to continue the development of this project and thus welcome all developers wishing to contribute via improving documentation, bug fixes, test coverage, new features, etc.

Installation

$ pip install django-firebase-orm

Initialize

In your project settings add the following configuration variables

settings.py

FIREBASE_ORM_CERTIFICATE = 'path/to/serviceAccountKey.json'
FIREBASE_ORM_BUCKET_NAME = '<BUCKET_NAME>.appspot.com'

FIREBASE_ORM_CERTIFICATE

: Once you have created a Firebase console project and downloaded a JSON file with your service account credentials.

FIREBASE_ORM_BUCKET_NAME

: The bucket name must not contain gs:// or any other protocol prefixes. For example, if the bucket URL displayed in the Firebase console is gs://bucket-name.appspot.com, pass the string bucket-name.appspot.com

Usage

Create model:

from firebase_orm import models


class Article(models.Model):
    headline = models.TextField()
    type_article = models.TextField(db_column='type')

    class Meta:
        db_table = 'medications'

    def __str__(self):
        return self.headline

Using The API:

Creating objects

To represent cloud firestore data in Python objects, FirebaseORM uses an intuitive system: A model class represents a collection, and an instance of that class represents a document in collection.

To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database.

# Import the models we created
>>> from models import Article
# Create a new Article.
>>> a = Article(headline='Django is cool')
# Save the object into the database. You have to call save() explicitly.
>>> a.save()

Retrieving all objects

The simplest way to retrieve documents from a collections is to get all of them. To do this, use the all() method on a Manager as you would in normal django:

>>> all_Article = Article.objects.all()

The all() method returns a list instance Article of all the collection in the database.

# Now it has an ID.
>>> a.id
1
# Fields are represented as attributes on the Python object.
>>> a.headline
'Django is cool'

Saving changes to objects

To save changes to an object that's already in the database, use save().

Given a Article instance a that has already been saved to the database, this example changes its name and updates its record in the database:

>>> a.headline = 'Django-Firebase-ORM is awesome'
>>> a.save()

This performs an document.update() method behind the scenes. FirebaseORM doesn't hit the database until you explicitly call save().

# Firebase ORM provides a rich database lookup API.
>>> Article.objects.get(id=1)
<Article: Django-Firebase-ORM is awesome>
>>> Article.objects.get(id=2)
Traceback (most recent call last):
    ...
DoesNotExist: Article matching query does not exist.

Field options:

The following arguments are available to all field types. All are optional.

Field.db_column

If contains characters that aren't allowed in Python variable names -- use db_column. The name of the firestore key in document to use for this field. If this isn't given, FirebaseORM will use the field's name.

Field types:

AutoField

class AutoField()

By default, FirebaseORM gives each model the following field:

id = models.AutoField(primary_key=True)

TextField

**class TextField(**options)**

Text string Up to 1,048,487 bytes (1 MiB - 89 bytes). Only the first 1,500 bytes of the UTF-8 representation are considered by queries.

TextField has not extra required argument.

Dependencies

  1. firebase-admin
  2. grpcio
  3. django

Building from Source for Developers

$ git clone https://github.com/TralahM/django-firebase-orm.git
$ cd django-firebase-orm

Contributing

See the Contributing File

See the Pull Request File

LICENCE

Read the license here

CREDITS

Thanks to joewalk102 for forking the original project without whom this project would not be possible.

TralahTek TralahM TralahM

Self-Promotion

TralahM TralahM

Blog

TralahTek

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-firebase-orm-0.5.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

django_firebase_orm-0.5-py36-none-any.whl (20.8 kB view details)

Uploaded Python 3.6

File details

Details for the file django-firebase-orm-0.5.tar.gz.

File metadata

  • Download URL: django-firebase-orm-0.5.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.6

File hashes

Hashes for django-firebase-orm-0.5.tar.gz
Algorithm Hash digest
SHA256 e94222249e5582a573753e34537f3a3148626ac63b5e2a552214fd452afa0f54
MD5 3e9d5fb973602afa987582b0981760bd
BLAKE2b-256 dbd3634ede5723e0e75b19003518c0f1bb4c472e036e6971b27bad5aea7688f1

See more details on using hashes here.

File details

Details for the file django_firebase_orm-0.5-py36-none-any.whl.

File metadata

  • Download URL: django_firebase_orm-0.5-py36-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.6

File hashes

Hashes for django_firebase_orm-0.5-py36-none-any.whl
Algorithm Hash digest
SHA256 17095c34e83a9baacb00e3b676b8a671ee29345e093863f65d7b3c055b7545bc
MD5 c43b492d88878edfe66d35e7bb9a134a
BLAKE2b-256 ad8355d1e15666308de22d80ab4738c23985b596b14cdb9755ba2729bbf0310d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page