Am trying to implement Firebase Realtime Database with my Django Web Application. After properly setting up the configuration with Firebase, I got confused about how data will write into my Firebase Database from my Django website instead of using Sqlite, or Postgres.
Under settings.py
, do I need to set my engine to Firebase? Am totally confused here. I do not want to use the normal ORM such as Sqlite, Postgres etc. I want my app to use Firebase.
Is there something else I need to understand about Firebase?
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
pyrebase_settings file
import pyrebase
config = {
"apiKey": "my_api_key_is_here",
"authDomain": "my_auth_domain_is_here",
"databaseURL": "my_firebase_database_url_is_here",
"projectId": "my_firebase_project_id_is_here",
"storageBucket": "my_firebase_storageBucket_is_here",
"serviceAccount": "my_serviceAccount.json_file_path_is_here",
"messagingSenderId": "messagingSenderId_is_here"
}
# initialize app with config
firebase = pyrebase.initialize_app(config)
# authenticate a user
auth = firebase.auth()
user = auth.sign_in_with_email_and_password("[email protected]", "FstrongPasswordHere")
db = firebase.database()
To connect Firebase and Django, we need to install a python package named pyrebase.
We currently support Python 3.6+. Firebase Admin Python SDK is also tested on PyPy and Google App Engine environments.
What is Django? Django is a Python framework that makes it easier to create web sites using Python. Django takes care of the difficult stuff so that you can concentrate on building your web applications.
Is there something else I need to understand about Firebase?
No, there is something you need to understand about Django -- it is designed to use a relational database engine as its primary data store. Firebase is not relational.
Sorry to be the bearer of bad news.
Many answers on this question are missing this point, and suggesting code or libraries that let you access Firebase from Python. Sure, that's possible. But that doesn't make it work as a primary data store for Django. Django's ORM features and contrib apps all presume a relational database.
In order for this to work you'd need something along the lines of django-nonrel that supported Firebase. As far as I know that doesn't exist.
take a look at the database section of the doc. It details how you would interact with the database API. Typically you would use these in your views where you need to save or retrieve something from the database.
e.g say you want a view that gets users, you could have something like this:
#views.py
from pyrebase_settings import db, auth
from django.shortcuts import render
def get_users(request):
users = db.child("users").get()
return render(request, 'users.html', {'users': users.val()})
The documentation for retrieving data can be seen here
also say you want a view to save(signup) users, you could have something like so:
#views.py
from pyrebase_settings import db, auth
def signup(request):
form = SignupForm(request.POST)
if form.is_valid():
email = form.cleaned_data('email')
password = form.cleaned_data('password')
auth.create_user_with_email_and_password(email, password)
# the rest of your code goes here
the create_user_with_email_and_password
method is documented here
PS. I have never made use of the pyrebase library so I write only based on what is specified in its docs. Also I am only writing these django snippets off the top of my head so forgive me if there are any syntax errors or typos :)
Hope this helps :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With