【问题标题】:How to implement FirebaseDB with a Django Web Application如何使用 Django Web 应用程序实现 FirebaseDB
【发布时间】:2017-04-28 13:26:10
【问题描述】:

我正在尝试使用我的 Django Web 应用程序实现 Firebase 实时数据库。在使用 Firebase 正确设置配置后,我对如何从 Django 网站而不是使用 Sqlite 或 Postgres 将数据写入 Firebase 数据库感到困惑。

settings.py 下,我需要将我的引擎设置为 Firebase 吗?我在这里完全困惑。我不想使用普通的 ORM,例如 Sqlite、Postgres 等。我希望我的应用使用 Firebase。

关于 Firebase,我还有什么需要了解的吗?

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

pyrebase_settings 文件

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()

【问题讨论】:

    标签: django firebase firebase-realtime-database


    【解决方案1】:

    查看文档的database 部分。它详细说明了您将如何与数据库 API 交互。通常,您会在需要从数据库中保存或检索某些内容的视图中使用它们。

    例如说你想要一个吸引用户的视图,你可以有这样的东西:

    #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()})
    

    获取数据的文档可以看here

    还说你想要一个保存(注册)用户的视图,你可以有这样的东西:

    #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 方法记录在 here

    PS。我从未使用过 pyrebase 库,所以我只根据其文档中指定的内容进行编写。另外,我只是在脑海中写下这些 django sn-ps,如果有任何语法错误或拼写错误,请原谅我:)

    希望这会有所帮助:)

    【讨论】:

    • 这没有回答问题。 OP 希望使用 Firebase 作为主要数据存储“而不是使用 Sqlite 或 Postgres”。 Stock Django 无法做到这一点。
    • @PaulBissex 我的回答没有提到 sqlite 或 postgres。我的回答从字面上解释了他们如何使用 pyrebase 和 django 视图
    【解决方案2】:

    关于 Firebase,我还有什么需要了解的吗?

    不,您需要了解一些关于 Django 的知识——它被设计为使用关系数据库引擎作为其主要数据存储。 Firebase 不是关系型的。

    很抱歉成为坏消息的承担者。

    关于这个问题的许多答案都忽略了这一点,并建议让您从 Python 访问 Firebase 的代码或库。当然,这是可能的。但这并不能作为 Django 的主要数据存储区。 Django 的 ORM 功能和贡献应用程序都假定关系数据库。

    为了使其工作,您需要支持 Firebase 的类似于 django-nonrel 的东西。据我所知不存在。

    【讨论】:

      【解决方案3】:

      考虑看看这里的 fcm-django 库https://github.com/xtrinch/fcm-django

      【讨论】:

      • 这没有回答问题。 OP 希望使用 Firebase 作为主要数据存储“而不是使用 Sqlite 或 Postgres”。
      【解决方案4】:

      我从 html 获取多个文件并将这些文件上传到 web django 中的 firebase 存储的解决方案

      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)
      

      【讨论】:

      • 这没有回答问题。 OP 希望使用 Firebase 作为主要数据存储“而不是使用 Sqlite 或 Postgres”。
      【解决方案5】:

      Pyrebase 是 Python 的 Firebase 的包装器。我不推荐使用 Pyrebase,因为它落后于对 Firestore 和 FCM 的支持。当 Django 中的一切都在服务器端完成时,为什么不在 Django 服务器上使用 Firebase Admin SDK。您还可以使用它管理用户。我的意思是有很好的文档可以将 Firebase 添加到我们的服务器。此外,他们还有用于服务器的 Python、NodeJS 等文档。我知道它有点像 API 的东西。但我觉得这是在 Django 中正确的做法,因为 Django 是一个服务器应用程序。看看这个link

      【讨论】:

      • 这没有回答问题。 OP 希望使用 Firebase 作为主要数据存储“而不是使用 Sqlite 或 Postgres”。
      • 请务必注意,Django 不直接支持 Firebase。但是,Firebase 支持 Python,您可以通过 Admin SDK 在服务器端使用它。我希望澄清。欲了解更多信息,请查看 JenPerson 的此视频:youtube.com/watch?v=yylnC3dr_no
      猜你喜欢
      • 2017-11-24
      • 1970-01-01
      • 2011-08-28
      • 2014-01-15
      • 1970-01-01
      • 2011-07-09
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多