Firebird database

Hi Django Forum.

I’m learning Django and I found it very difficult to configure the Firebird database in Settings.

I tried several libraries and none worked.

My database configuration:

DATABASES = {
‘default’: {
‘ENGINE’ : ‘django.db.backends.firebird’,
‘NAME’ : ‘C:/Projects/data/database.fdb’,
‘USER’ : ‘SYSDBA’,
‘PASSWORD’ : ‘masterkey’,
‘HOST’ : ‘127.0.0.1’,
‘PORT’ : ‘3050’
}
}

The lasts errors was this:

raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: ‘firebird.backend’ isn’t an available database backend.
Try using ‘django.db.backends.XXX’, where XXX is one of:
‘mysql’, ‘oracle’, ‘postgresql’, ‘sqlite3’

conn = backend.DatabaseWrapper(db, alias)
AttributeError: module ‘firebird.base’ has no attribute ‘DatabaseWrapper’

from django.utils.asyncio import async_unsafe
ModuleNotFoundError: No module named ‘django.utils.asyncio’

Could someone please help me with how to do this?

Thank you very much in advance.

Firebird is not a supported database by Django. There might be a third-party package which interfaces with Django.

Edit: Looks like there is: GitHub - maxirobaina/django-firebird: Firebird SQL backend for Django web framework

Is there a particular reason you are using Firebird?

Yes. I work at a company whose main database is Firebird.

I’m trying to implement a data analysis module (dasboards), but it’s being very difficult to integrate Firebird with Django.

Right ok.

As per my first message, I would suggest installing the package linked and see how you get on following the instructions.
Any further issues in regards to firebird are more likely to yield better results by raising an issue or other relevant mechanism on that repository.

Hi Brodinho,

I have a similar issue, i.e. same error, but with a different setup. I use django in docker, but that should not make a big difference. What I found out is, that you have to install some packages to python:

  /py/bin/pip install django-utils-six && \
 #   /py/bin/pip install fdb && \
    /py/bin/pip install firebird-driver && \
    /py/bin/pip install django-firebird && \
    /py/bin/pip install firebird-lib && \

The utils-six is no longer included in the current python version, so you need to install that. Then the django-firebird need to be installed, which requires fdb accordance to the documentation. However fdb is replaced by firebird-driver. So that’s the reason for installing the latter.
I am not sure, whether I also need to install firebird-lib, but I also installed this.

The DATABASE setup should use another ENGINE Entry:

{
         'ENGINE' : 'firebird',
         'HOST': ...,
         'NAME':  ...,
         'USER': ...,
         'PASSWORD': ...,
         'PORT': '3050',
         'OPTIONS': {
             'charset':'ISO8859_1'
         },

With this setup I still receive the Error AttributeError: module ‘firebird.base’ has no attribute ‘DatabaseWrapper, but no longer the error django.core.exceptions.ImproperlyConfigured: ‘firebird.backend’ isn’t an available database backend.

Habe jetzt mal auf ältere Versionen aufgesetzt (über Docker).
Nutze Python 3.9.20, Firebird 3, Django 4.2.16
Damit habe ich versucht über Python direkt auf die Firebirddatenbank zuzugreifen:

from firebird.driver import connect, DatabaseError
from firebird.driver import driver_config

try:
    driver_config.server_defaults.host.value = '172.17.0.3'
    driver_config.server_defaults.user.value = 'SYSDBA'
    driver_config.server_defaults.password.value = 'masterkey'
    con = connect('/firebird/data/employeefb3.fdb')
    print("Connected to /firebird/data/employeefb3.fdb")
    cursor = con.cursor()
    cursor.execute("SELECT * FROM EMPLOYEE")

    result = cursor.fetchall()
    for data in result:
        print(data)
except DatabaseError as e:
    print(f"Database Error: {e}")
finally:
    con.close()

Das funktioniert und es kommt beim Ausführen folgender Output:

(2, 'Robert', 'Nelson', '250', datetime.datetime(1988, 12, 28, 0, 0), '600', 'VP', 2, 'USA', Decimal('105900'), 'Nelson, Robert')
(4, 'Bruce', 'Young', '233', datetime.datetime(1988, 12, 28, 0, 0), '621', 'Eng', 2, 'USA', Decimal('97500'), 'Young, Bruce')
(5, 'Kim', 'Lambert', '22', datetime.datetime(1989, 2, 6, 0, 0), '130', 'Eng', 2, 'USA', Decimal('102750'), 'Lambert, Kim')
(8, 'Leslie', 'Johnson', '410', datetime.datetime(1989, 4, 5, 0, 0), '180', 'Mktg', 3, 'USA', Decimal('64635'), 'Johnson, Leslie')
(9, 'Phil', 'Forest', '229', datetime.datetime(1989, 4, 17, 0, 0), '622', 'Mngr', 3, 'USA', Decimal('75060'), 'Forest, Phil')
(11, 'K. J.', 'Weston', '34', datetime.datetime(1990, 1, 17, 0, 0), '130', 'SRep', 4, 'USA', Decimal('86292.94'), 'Weston, K. J.')
... 

Aber wenn ich den runserver starte kommt immer noch die Meldung:

AttributeError: module ‘firebird.base’ has no attribute ‘DatabaseWrapper’

Habe Django-firebird und firebird-driver (anstatt fdb) mit pip installiert.

Hat noch jemand eine Idee, woran es liegen könnte?

Hello Same problem at me ,i wrote about it to author of django-firebird

direct connection working

import fdb

con = fdb.connect(dsn='//172.16.**.**/c:/database/db.fdb',user=******',password='******')
print("Připojeno k databázi FireBird "'\n')

From: Me.
Sent: Friday, December 6, 2024 1:41 AM
To: maxirobaina@gmail.com
Subject: cant run firebird db with django

Hello MaxiRobaina

I cant run FB in Django,
please can you help me with it?
It is possible run FB 3 with Django?

Firebird is 3.0.4 and working with my python console

Python 3.12
Django 4.2.1
django-firebird 2.2.2
firebird-lib 1.17.0
fdb 1.0.0
django-utils-six 2.0

in django i recive
Request Method: GET
Request URL: http://127.0.0.1:8000/Pristup/

Django Version: 4.2.1
Exception Type: AttributeError
Exception Value: module ‘firebird.base’ has no attribute ‘DatabaseWrapper’

In settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    },
        'apotest': {
        'ENGINE' : 'firebird',
        'NAME' : 'c:/database/db.fdb', # Path to database or db alias
        'USER' : '******',           # Your db user
        'PASSWORD' : '******',    # db user password
        'HOST' : '172.16.**.**',        # Your host machine
        'PORT' : '3050',             # If is empty, use default 3050
        #'OPTIONS' : {'charset':'ISO8859_1'}
    }

Your sencerely

Ladislav

I dug into the source code and found the following problem:
django-firebird tries to load the module firebird.base and this does not work, because there is a `

/firebird/base.py

` file and a

/firebird/base

folder. The import of firebird.base checks the folder and cannot find the class DatabaseWrapper, because this is defined in the base.py file. I renamed the base folder to /firebird/newBase. Now the system loads the module and I can at least start the development server with runserver.

But this solution was just for testing. The django documentation shows, that a package is imported in one of two ways. If a folder contains a file init().py then this folder is assumed to be a package. Therefore a better soultion is to rename or delete the file

/firebird/base/init.py

and then put the old name of the folder base back into place (so rename newBase to base).
In my case it still works. I could already migrate data to my firebird3.0 table.

By the way, I am using python 3.9.21 and django 2.2.28.emphasized text

I try Your solutin, rename from

...\python\.venv\Lib\site-packages\firebird\base

to

...\python\.venv\Lib\site-packages\firebird\newbase

, but dont working
i have now this error message

ImproperlyConfigured at /PristupApotest/

'firebird' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'

Request Method: 	GET
Request URL: 	http://127.0.0.1:8000/PristupApotest/
Django Version: 	4.2.1
Exception Type: 	ImproperlyConfigured
Exception Value: 	

'firebird' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'
Error during template rendering

In template ...\python\.venv\Scripts\eissite\reports\templates\listApotest.html, error at line 14
'firebird' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3'

in Traceback

The above exception (cannot import name 'six' from 'django.utils' (...\python\.venv\Lib\site-packages\django\utils\__init__.py)) was the direct cause of the following exception: 

before I try Your solution I have this

AttributeError at /PristupApotest/

module 'firebird.base' has no attribute 'DatabaseWrapper'

Request Method: 	GET
Request URL: 	http://127.0.0.1:8000/PristupApotest/
Django Version: 	4.2.1
Exception Type: 	AttributeError
Exception Value: 	

module 'firebird.base' has no attribute 'DatabaseWrapper'

In relation to Your post, Also I try solved problem with copy base.py to give Database Wrapper class for firebird.base impots, that its may needed
from

...\python\.venv\Lib\site-packages\firebird\base.py

to

...\python\.venv\Lib\site-packages\firebird\base\base.py

And repaired some path in import, but nothing change, still same error

module 'firebird.base' has no attribute 'DatabaseWrapper'

Where may i put DatabaseWrapper class, in which file?
please can You little bit more explain mechanism about django-firebird tries?

So next I try to add contain of

...\python\.venv\Lib\site-packages\firebird\base.py

to

...\python\.venv\Lib\site-packages\firebird\base\__init__.py

obtained new error about missing six in

...\python\.venv\Lib\site-packages\django\utils\__init__.py

so i add inside this file

import six

and now i have this error

ImproperlyConfigured at /PristupApotest/

'firebird' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'

Traceback

The above exception (cannot import name 'force_text' from 'django.utils.encoding' (...\python\.venv\Lib\site-packages\django\utils\encoding.py)) was the direct cause of the following exception

so at stackowerflow i found
https://stackoverflow.com/questions/70382084/import-error-force-text-from-django-utils-encoding

import django
from django.utils.encoding import force_str
django.utils.encoding.force_text = force_str

and put it in

...\python\.venv\Lib\site-packages\django\__init__.py

now i have mistake abot table, than so i think firebird is connected in django or it is near

DatabaseError at /PristupApotest/

('Error while preparing SQL statement:\n- SQLCODE: -204\n- Dynamic SQL Error\n- SQL error code = -204\n- Table unknown\n- REPORTS_PRISTUP\n- At line 1, column 70', -204, 335544569)

Request Method: 	GET
Request URL: 	http://127.0.0.1:8000/PristupApotest/
Django Version: 	4.2.1
Exception Type: 	DatabaseError
Exception Value: 	

('Error while preparing SQL statement:\n- SQLCODE: -204\n- Dynamic SQL Error\n- SQL error code = -204\n- Table unknown\n- REPORTS_PRISTUP\n- At line 1, column 70', -204, 335544569)

#iam sorry i have limitation 3 posts beause i am new so i write here:

now i get error

TypeError at /PristupApotest/

'PRISTUP' object is not iterable

Request Method: 	GET
Request URL: 	http://127.0.0.1:8000/PristupApotest/
Django Version: 	4.2.1
Exception Type: 	TypeError
Exception Value: 	

'PRISTUP' object is not iterable

response of server is

'query_results': <QuerySet [<PRISTUP: PRISTUP object (63483)>]>}]

i cant solve it now, may be problem this dont solve my prob

.filter(ID=63483)
still get object object (63483) as result, but it is only str …

Depending on the python version six might no longer be available. You have to execute

pip install six

and I also added

pip install django_utils_six

Then this error should be fixed.

But as I said, renaming the folder was just for testing. I recommend to rename /firebird/base/init.py e.g. to /firebird/base/init__.py. In this case you can leave the firebird-base driver in its original folder.

You don‘t need to change anything it the firebird-driver files.

You also might consider the python and django versions that I used: 3.9.21 and 2.2.28. With these versions you don’t have to change any files.

hello

I’ve promoted up a trust level!

so I can write reply to you here

I have and like to have Django 4.2
But still presist mistake, may be update to django-firebird needed
error demontrate here on result in my test page

query_results1 = PRISTUP.objects.get(ID=63483)   : PRISTUP object (63483)
query_results2 = PRISTUP.objects.filter(ID=63483): <QuerySet [<PRISTUP: PRISTUP object (63483)>]>
         query = query_results2.query.__str__()  : SELECT "PRISTUP"."IDPRISTUPU", "PRISTUP"."ZOBRAZJMENO", "PRISTUP"."HESLO" FROM "PRISTUP" WHERE "PRISTUP"."IDPRISTUPU" = 63483        
      response = print(query_results2)           : None

Hi tcladin,
currently I do have a running system with Django 2.2.28. I will in the near future bump up the versions of python and django and see how long it works. The latest versions with Django 5,1 and python 3.13 currently do not work.

But in your description, you did not follow my advice:

In your standard installation on Django just rename the file /firebird/base/init.py and make no other changes. I think you have installed six as described above.

What error does occur under these conditions?

I just tried to bump up django from 2.2.28 to 3.2.25, but it throws error in the database modules. So without changes in django-firebird you cannot go higher then 2.2.28.