In my database, I have a Customer
table defined in my database that all other tables are foreign keyed on.
class Customer(models.Model):
...
class TableA(models.Model):
Customer = models.ForeignKey(Customer)
...
class TableB(models.Model):
Customer = models.ForeignKey(Customer)
...
I'm trying to implement a database router that determines the database to connect to based on the primary key of the Customer
table. For instance, id
s in the range 1 - 100 will connect to Database A, id
s in the range 101 - 200 will connect to Database B.
I've read through the Django documentation on routers but I'm unsure if what I'm asking is possible. Specifically, the methods db_for_read(model, **hints)
and db_for_write(model, **hints)
work on the type of the object. This is useless for me as I need routing to be based on the contents of the instance of the object. The documentation further states that the only **hints
provided at this moment are an instance
object where applicable and in some cases no instance
is provided at all. This doesn't inspire me with confidence as it does not explicitly state the cases when no instance
is provided.
I'm essentially attempting to implement application level sharding of the database. Is this possible in Django?