![]() |
Django stands out as a powerful and versatile framework for building dynamic and scalable applications. One of its notable features is its robust support for database management, offering developers flexibility in choosing and integrating various database systems into their projects. In this article, we will see how to use Multiple Databases with Django. Using Multiple Databases with DjangoHere, we will see step-by-step how to connect Using Multiple Databases with Django in Python: Starting the Project FolderTo start the project use this command django-admin startproject core To start the app use this command python manage.py startapp home Now add this app to the ‘settings.py’ INSTALLED_APPS = [ Install required packages pip install djangorestframework File StructureSetting Necessary Filesmodels.py : Below, code defines a Django model called Paragraph with fields for a user reference, name, and description, using Django’s user model and settings for flexibility.
views.py : This Python code uses Django REST Framework to create a viewset for the Paragraph model, enabling CRUD operations (Create, Retrieve, Update, Delete) through API endpoints. It includes filtering capabilities based on the paragraph name and description fields.
serializers.py: This Python code creates a serializer for the Paragraph model, using Django REST Framework, to handle conversion to and from JSON format for all fields of the model.
home/urls.py: This Django code sets up URL patterns for the ParagraphViewSet, utilizing Django REST Framework’s DefaultRouter to enable CRUD operations on the ‘Paragraph’ model via ‘/recipes/’.
core/urls.py: This Django code defines URL patterns, routing requests to the admin interface at ‘/admin/’ and including URLs from the ‘home’ app at the root path.
admin.py Here we are registering our models.
Configure Django settings for PostgreSQL update below code in settings.py file
Configure Django settings for MySQL update below code in settings.py file
Deployment of the ProjectRun these commands to apply the migrations: python3 manage.py makemigrations For create the superuser use the below command : python3 manage.py createsuperuser Run the server with the help of following command: python3 manage.py runserver for flush the databse use the below command python3.manage.py flush Output |
Reffered: https://www.geeksforgeeks.org
Django |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |