Django多租户架构 - 所有模型都应该引用租户ID

时间:2010-03-04 23:44:17

标签: django database-design django-models

假设我的SAAS中的帐户属于Account(models.Model)类型。以下是一个好主意吗?

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(Account)
    mymodel = models.ForeignKey(MyModel)
    eggs = models.CharField(max_length=255)

1 个答案:

答案 0 :(得分:1)

如果你现在没有使用它,我会把它留下来。编码你现在需要的东西,如有必要,稍后重构。有一些工具可以使架构更改变得更加轻松。 South是一个很好的例子 - 在很多情况下运作良好,不断成熟,并且背后有很好的社区支持。 django-evolution是另一种选择它已经存在了一段时间,它的发展已经下降,但它提供了一些人仍然青睐的方法。

相关问题