0

Can I have a Django Instance of a project(one setting file) but different database per model or model instance? What do I mean.

Suppose we have a model Business

class Business(models.Model):
    #buisness specific fields

A model Employer

class Employer(models.Model):
    business = models.ForeignKey(Business)
    #other fields

Then I create 3 businesses

business1 = Business.create(#fields kwords heare)
business2 = Business.create(#etc)
business3 = Business.create(#etc)

I want each business to be stored to a different database but on the same server say business1, business2 and business3 respectively.

I also want to have employers but each employer object to be stored to the same data base of its foreign key, so

employer1 = Employer.create(business=business1, #...etc) #store it on table on business1 db
employer2 = Employer.create(business=business2, #....etc) #store it on table on business2 db
employer3 = Employer.create(business=business3, #....etc) #store it on table on business3 db.

Is this possible with Django?

Maximas
  • 702
  • 2
  • 6
  • 16
Apostolos
  • 7,763
  • 17
  • 80
  • 150
  • How would Django know which db to use when retrieving an object (or even worse - a list of objects)? – Ludwik Trammer Jul 10 '14 at 11:55
  • Yes that's what I thought after I posted the question. I read this (http://goo.gl/slJrtK) but that only counts for when db's are pre-defined in settings file. So I guess it probably isn't possible – Apostolos Jul 10 '14 at 12:02
  • 1
    Why do you want to do this ? What is your real problem ? – bruno desthuilliers Jul 10 '14 at 12:21
  • I wanted each buisness to have each own database. Because this project will hold sensitive data I was wondering if it is more secure to have each buinsess on a database on its own but running one instance of django. I am little concerend of security issues, since its my first big project – Apostolos Jul 10 '14 at 12:26
  • You can find information about having multi database in [Django doc](https://docs.djangoproject.com/en/dev/topics/db/multi-db/#cross-database-relations) – trnsnt Jul 10 '14 at 12:31
  • Yes I allready comented on that, multiple databases in django need to be pre-defined, and then routed, but predefined, I was talking to creating a database at model creation. But I guess it's not possible. – Apostolos Jul 10 '14 at 12:37
  • 1
    You can have a look at this [post](http://stackoverflow.com/a/18016960), it can be helpful! – trnsnt Jul 10 '14 at 12:38

0 Answers0