Assuming you have multiple databases in your django project for different apps. Now you realize that when you save or do other operations in admin site for your app model , you realize that values are save to default database from project.
Below we will see simple steps to make admin site of django to use your other database configured for the app instead of default.
Now your multiple database in settings.py may looks something like this
settings.py |
So inorder to inform the django admin site use your intendended database for your app.
Navigate to your app in the django project and open your admin.py. Below example shows model names ProfileSection which when the user saves the information in admin site uses default database.
admin.py |
If you want to provide an admin interface for a model on a database other than that specified by your router chain, you’ll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.
You can now create a ModelAdmin object in your admin.py like below. Also mention which database to be used in the "using" keyword
ModelAdmin object in admin.py |
Once you have created your custom ModelAdmin setup . You can now proceed to register your model in admin site to use your other database. In my case "resumeapp" will be used.
Registering model in admin .py |
You are all set . Now when you save or do other operation in admin site on your model in the app , Django will use database mentioned in "using" keyword.
Hope this was helpful.!!
Comments
Post a Comment