2

I have different data databases of same database schema (also in 1 database: different tables with same structure/schema) and I want use those databases in all my other apps as data backend.

For example, database name: database1

class tableA(models.Model):
    a = models.CharField()
    b = models.CharField()
    class Meta:
        db_table = 'tableA'

class tableB(models.Model):
    c = models.CharField()
    d = models.CharField()
    class Meta:
        db_table = 'tableB'

class tableC(models.Model):
    c = models.CharField()
    d = models.CharField()
    class Meta:
        db_table = 'tableC'

database name: database2

class tableA(models.Model):
    a = models.CharField()
    b = models.CharField()
    class Meta:
        db_table = 'tableA'

class tableB(models.Model):
    c = models.CharField()
    d = models.CharField()
    class Meta:
        db_table = 'tableB'

class tableC(models.Model):
    c = models.CharField()
    d = models.CharField()
    class Meta:
        db_table = 'tableC'

Here, you can see database1 and database2 is having same schema. Also in both databases, tables: tableB and tableC having same schema. In short there is seperate database created for each region with same structure instead of 1 big database for all regions. In 1 database I have around 15 tables and out of 15, 12 tables having same schema in which daily data is stored.

Can anyone please tell me how should I design this in django? Should I create 1 app with multiple model files (1 for each database) and direct it to different databases with router? How? Or create different app for each database? You can see in both cases there is a lot of code redudency as all model files having same structure.

  • What is this? a multi-tenant application(where each tenant requires a separate db instance) or is it a project requiring a db split into multiple shards? – Crazyshezy Aug 26 '14 at 13:43
  • actually, I have databases from different institutions to read ozone, CO values everyday. I have data from 1970. For example I have DatabaseA for instituteA to store it's ozone, CO values and DatabaseB for InstituteB to store it's ozone, CO values...etc So my databases structure is same. I want to filter this data with institute, species(ozone, co), time period, etc. from web client. I dont know how to write model for it with minimum code redundancy. – user2656976 Aug 28 '14 at 10:51
  • Currently I created seperate app for each database and accessing it from web client. but I can say that my all database apps are same except database and app name. I am using django rest framework. – user2656976 Aug 28 '14 at 10:51
  • Ok i understand that you have similar data coming from different places.. now what is your objective.. do u wanna merge this data into one or keep it separate? I would suggest you to merge this data in a single db (so you can work on the data as a whole).. also maintain the date-time and where the data is coming from along with the data. which will help you categorize this data base on institutes/date/time etc – Crazyshezy Aug 28 '14 at 11:39
  • Actually the problem is they are handled by different department and its easy for them to have a separate database for each. So I can not ask them to combine it. Is there any way in django to have single app with multiple databases may be using router.py? I tried to create single app with multiple model files but I could not route them to different databases as there is no any option for specifing database like app_label in Meta options. – user2656976 Aug 28 '14 at 12:07
  • https://docs.djangoproject.com/en/dev/topics/db/multi-db/ – Crazyshezy Aug 28 '14 at 12:43

0 Answers0