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?.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With