Django database for big data

Hello,
is there any way to use two SQLite databases in the same project?

I have big data with many tables so I want to separate the database in my Django project.

For example, in this table, I have 125622 rows and 15 columns.

acccc

Could I use access DB with Django for such data?
because I need this format to be loaded in other software.
or could I export or add new entries to an existing access DB using Django?
Thanks

This is not “big data”. Multiply that by 10 and you’re starting to get close to something you could call big data.

See Multiple databases | Django documentation | Django for handling multiple databases. You can even access different types of databases using that facility. (For example, I’ve got a couple of systems where the Django data is in sqlite, but the “real” data is in PostgreSQL. The system handles both of them seamlessly.)

Django does not natively support Access DBs, but you might be able to find something that works. (Don’t personally know, have never tried.) See Databases | Django documentation | Django for the official list of supported databases.

1 Like

Many thanks for the responses

(for big data the example was just one table of many tables I just entered one table on the SQLite file that reaches 20Mb size, and I have tens of such table) .

I followed the django instructions for additionelle database, and in my settings.py I added:

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

And I run this command :

./manage.py migrate --database=measure

So I created the new database, but it cloned all the old db tables?
I don’t need to creat another copy of tables, how to add and use the new database in modes.py, views.py like the old one even in the Admin page?

My best

1 Like

See the Synchronizing your Databases and Automatic database routing sections on that same doc page. (You probably should read the entire page.)

I had created a new database and configured my files as in the Django documentation.
now I can access to the database via the Admin page and add some tables and data.
this is my settings.py configurations.

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

DATABASE_ROUTERS =['routers.db_routers.AuthRouter','routers.db_routers.Measure']

and I created a folder (routers) in it another file db_routers.py that contains this code:


from os import read
class AuthRouter:
    route_app_labels = {'auth', 'contenttypes','sessions','admin'}

    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'default'
        return None
    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'default'
        return None
    
    def allow_relation(self, obj1, obj2, **hints):
        if (
            obj1._meta.app_label in self.route_app_labels or
            obj2._meta.app_label in self.route_app_labels
        ):
            return True
        return None
    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'default'
        return None


class Measure:
    route_app_labels = {'measure'}
    def db_for_read(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'measure_db'
        return None
    def db_for_write(self, model, **hints):
        if model._meta.app_label in self.route_app_labels:
            return 'measure_db'
        return None
    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label in self.route_app_labels:
            return db == 'measure_db'
        return None

then I created a new app measure and in its models.py I add this table

class SurveillanceDesPuits(models.Model):
    PUITS=  models.CharField(max_length=15)
....
.....
....
    post_date   = models.DateTimeField(default=timezone.now)
    def __str__(self):
        return self.PUITS

I saved in the admin.py

then I run :

> python manage.py makemigrations
> python manage.py migrate --database=measure_db

so the same issue it creates a new database named measure.db but it creates all tables in the first database+ the new table named SurveillanceDesPuits?
could I delete those tables manually or there are another tips and tricks?

All my best

Looking at your allow_migrate function(s):

If you have any apps that aren’t listed in route_app_labels, they will be migrated in both - lets say you have a new app called “new_app”. Since it’s not in either route_app_labels set, both if statements will be false, meaning both functions will return None - in which case the migration will be allowed to proceed.

so I added the unwanted database to route_app_labels as:

route_app_labels = {'auth', 'contenttypes','sessions','admin','Home'}

I checked and it works very well.

however, in the new database, there is a migration table that is logical and is related to the new database’s migration. but the new user_profile table what is its role?

Thanks a lot Mr Ken

I don’t find a “Profile” class in any of the standard packages. It’s either being created in one of your modules or by some other 3rd party package.

1 Like

yes, you are right, think I should add all apps in route_app_labels, I have a user app.