I have a django project where I am trying to make a customer based grocery system. Here I have different databases for each customer. Each database consist of order history as well as all other details like contact details etc.
I will get an url argument to identify customer through customer_id & I will able to get which database to use ie db name. Code -
Model -
class Company(models.Model):
company_id = models.IntegerField(primary_key = True)
name = models.CharField(max_length=256, blank=True, null=True)
db_name = models.CharField(max_length=200, blank=True, null=True)
db_uuid = models.CharField(max_length=100, blank=True, null=True)
objects = DataFrameManager()
def __str__(self):
return self.name
Views file -
@api_view(['GET', 'POST'])
def page(request, uuid):
if request.method == 'GET':
rtrn = Company.objects.filter(db_uuid=uuid).values('db_name')
return Response(rtrn)
Url file -
urlpatterns = [
url(r'user/(?P<uuid>[a-zA-Z0-9-]+)/$', page, name="getfromurl"),
]
Here I will return db name.
But I am not able to understand and implement the change of database. Let me make you understand with an example -
Lets suppose Customer name Gaurav has db 'gaurav_groceries'. By the code I get the name - gaurav_groceries. But in setting.py I have added default db as 'grocies', so how can I use this dynamic nature of db ie in this case we have 'gaurav_groceries', for further calculation and coding.
Thank you