[Answered ]-Django Rest Framework with Firebase Firestore โ€“ API endpoint returns NoneType due to initialize_app runs more than once

1๐Ÿ‘

โœ…

I solved my problem by inserting my firebase initializer code piece into manage.py. Plus, it also works in settings.py.

For example, the manage.py file can be rearranged as follows:

import os, sys
import firebase_admin
from firebase_admin import credentials, firestore

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
    try:
        from django.core.management import execute_from_command_line
        if not firebase_admin._apps:
            cred = credentials.Certificate('./FILE_PATH.json') 
            firebase_admin.initialize_app(cred)
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

or you can simply add the following lines to anywhere in settings.py:

import firebase_admin
from firebase_admin import credentials

if not firebase_admin._apps:
    cred = credentials.Certificate('./FILE_PATH.json') 
    firebase_admin.initialize_app(cred)

I hope this answer helps others.

๐Ÿ‘คOnur Baskin

Leave a comment