Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide a large Django project into sub projects for scaling?

Django project(e-commerce website) that we need to divide into sub-projects as a Buyer, Seller, and Admin and will create three databases accordingly. So how can we manage the same Models(Schema) in 3 projects if it is updated in one project?.

like image 950
Afsal KK Avatar asked Nov 07 '22 06:11

Afsal KK


1 Answers

Django support multiple database features for same project.

This can be achieved by defined multiple database property in settings.py as

DATABASES = {
    'default': {},
    'buyer': {
        'NAME': 'buyer',
        'ENGINE': 'django.db.backends.mysql',
        'USER': '',
        'PASSWORD': ''
    },
    'customers': {
        'NAME': 'customers',
        'ENGINE': 'django.db.backends.mysql',
        'USER': '',
        'PASSWORD': ''
    }
}

Need to define router along with this. https://docs.djangoproject.com/en/3.0/topics/db/multi-db/#using-routers

Also manually can be selected database by using keyword eg.

## fetch
Customer.objects.using('customers').all()
## save
customer_obj.save(using='customers')

Ofcourse there is some limitation with multiple database such as

  • Cross-database relations

    Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases.

  • Behavior of contrib apps

    Several contrib apps include models, and some apps depend on others. Since cross-database relationships are impossible, this creates some restrictions on how you can split these models across databases: https://docs.djangoproject.com/en/3.0/topics/db/multi-db/#behavior-of-contrib-apps

The official link can be followed for more details:

https://docs.djangoproject.com/en/3.0/topics/db/multi-db/

like image 57
Furkan Siddiqui Avatar answered Nov 14 '22 09:11

Furkan Siddiqui