I'm trying to find the right way to do this:
Users service:
/api/<country>/users
/api/us/users
That service should use the database corresponding to the country in the URL.
settings.py:
DATABASES = {
'default': {},
'us': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'XXX_US',
'USER': 'US',
'PASSWORD': 'XXX',
'HOST': 'localhost',
'PORT': '5432',
},
'es': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'XXX_ES',
'USER': 'ES',
'PASSWORD': 'XXX',
'HOST': 'localhost',
'PORT': '5432',
} }
To set the database in the ModelViewSet I to this:
class UserViewSet(viewsets.ModelViewSet):
model = User
serializer_class = UserSerializer
def get_queryset(self):
country = self.kwargs['country']
return User.objects.using(country).all()
The problem appears when I try to do a POST or PUT. Do I have to overwrite the create() or save() method of the serializer? Is there any other way to do this?
Thank you very much!
I think the best place for such functionality is a
QuerySet
or a ModelManager. For example, the DRF's default serializer uses the default model's manager for creating objects. Unfortunately, theQuerySet
doesn't have a way to easily change the current database (self.db
) depending on the models' fields, so you'll have to override all the relevant methods.Thank you Alex! You answer help me a lot. To fully solve the problem I have to do this:
views.py
And then in the models.py:
And the problem is solved! Thank you a lot.