'Django-rest-framework - fetching data from another server's DB
I've implemented API using Django REST Framework that I use for my project which is a Flutter app.
*BUT, I want to add some data from another Server's database, can I do it in Django REST Framework, and then include those in my API ?
Solution 1:[1]
You can set multiple databases in settings.py file.
DATABASES = {
'default': {
...
},
'other': {
...
}
}
And you need to create another app other and define the models in models.py of the newly created project folder. Let's say, you defined a Sport model in other app.
Then in views.py file you can refer to this model.
from other.models import Sport
# in one of your api view
def SomeView(...):
...
Sport.objects.using('other').create(...)
The main code is using('...').
Note: You don't need to make migrations for the other app when you need to make migrations.
Hope it could help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Lu |
