How to implement FirebaseDB using a Django web application

I am trying to implement a Firebase Realtime database using my Django web application. After configuring correctly with Firebase, I was confused about how the data would be written to my Firebase database from my Django site, instead of using Sqlite or Postgres.

In settings.py do I need to install my engine in Firebase? Here I am completely confused. I do not want to use regular ORMs like Sqlite, Postgres, etc. I want my application to use Firebase.

Is there anything else I need to know 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@usedforauthentication.com ", "FstrongPasswordHere") db = firebase.database() 
+12
source share
5 answers

take a look at the database section of the document. It describes in detail how you will interact with the database API. Usually you use them in your views, where you need to save or retrieve something from the database.

For example, if you want a view to receive users, you might 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 obtaining data can be found here.

let's also say that you want the view to save (register) users, you might have something like this:

 #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 

create_user_with_email_and_password method here

PS. I have never used the pyrebase library, so I only write based on what is indicated in its documentation. Also, I only write these django snippets on my head, so I'm sorry if there are any syntax errors or typos :)

Hope this helps :)

+10
source

consider looking at the fcm-django library here https://github.com/xtrinch/fcm-django

+2
source

Pyrebase is a shell for Firebase for python. I do not recommend using Pyrebase because it lags behind Firestore and FCM support. When everything in Django is done on the server, why not use the Firebase Admin SDK on the Django server. You can also manage users with it. I mean, there is good documentation for adding Firebase to our server. In addition, they have documents in Python, NodeJS, etc. For the server. I know this looks like an API. But I feel that this is the right way in Django, since Django is a server application. Check this link

0
source

Is there anything else I need to understand about Firebase?

No, you need to understand something about Django - it is intended to use the relational database engine as the main data warehouse . Firebase is not relational.

Sorry to bring bad news.

Many answers to this question overlook this point and offer code or libraries that allow you to access Firebase from Python. Of course it is possible. But that does not make it the main data warehouse for Django . All Django ORM features and add-on applications assume a relational database.

For this to work, you need something like django-nonrel that Firebase supports. As far as I know that does not exist.

0
source

My solution is to get multiple files from HTML and upload these files to Firebase repository in web Django

index.html

 <div id="fine-uploader-manual-trigger"> <input class="custom-file" type="file" name="imgname" id="productImgUpload" accept=".xlsx,.xls,image/*,.doc,audio/*,.docx,video/*,.ppt,.pptx,.txt,.pdf" multiple> </div> 

views.py

 from firebase import Firebase def storeimg(request): config = { "apiKey": "", "authDomain": "", "databaseURL": "", "storageBucket": "" } firebase = Firebase(config) storage = firebase.storage() finalimglist = [] imglist = [] for i in range(len(filepath)): storage.child("Product/"+cname+"/"+str(filepath[i])).put(filepath[i]) imgurl = storage.child("Product/"+cname+"/"+str(filepath[i])).get_url(token='null') imglist.append(imgurl) finalimglist.append(imglist) 
0
source

Source: https://habr.com/ru/post/1267284/


All Articles