Django multi tennant architecture - Should all models have a reference to the tennant_id
Let's say that accounts in my SAAS are of the type Account(models.Model)
. Would the following be a good idea?
class MyModel(models.Model):
account = models.ForeignKey(Account)
spam = models.CharField(max_length=255)
class MyOtherModel(models.Model):
# The next attribute `account` is the line in question.
# Should it be included even though mymodel.account can get the same value?
# The architecture could change later, and I might regret not including it,
# but I can't think of many reasons why, other than filtering a list out of
# this model like MyOtherModel.objects.filter(account=some_account).all()
# Are there other considerations?
account = models.ForeignKey(Acc开发者_如何学运维ount)
mymodel = models.ForeignKey(MyModel)
eggs = models.CharField(max_length=255)
If you don't have a use for it now I'd leave it out. Code what you need now, refactor if necessary later. There are a few tools out there that make schema changes more painless. South is a great example - works well in many situations, continues to mature, and has great community support behind it. django-evolution is another option It has been around for a while, development on it has dropped off, but it offers an approach that some people still favor.
精彩评论