在多租户Django设置中处理唯一性 [英] Handling uniqueness in a multi-tenant Django setup

查看:162
本文介绍了在多租户Django设置中处理唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有一个多租户Django数据库.我所有启用多租户的模型都从类AccountSpecificModel导入,该类作为FK导入到类Account.现在,我在Meta中有一些unique=Trueunqiue_together,它们没有指定account.

I ahve a multi tenant Django database. All my multi tenant enabled models import from a class AccountSpecificModel, which as a FK to a class Account. Now I have some unique=True and unqiue_together in Meta, which do not specify account.

在我的情况下,对于AccountSpecificModel,唯一性除非进行了account的处理,否则没有任何威胁,这意味着我想将每个unique转换为带有accountunique_together,并为unique_together提供类似的转换.

In my scenario, for AccountSpecificModel uniqueness has no menaing unless it takes care of account, which means I want to convert each unique to a unique_together with account, and simlar for unique_together.

我该怎么做?

推荐答案

如果我对您的理解正确,那么这就是你想要的:

If I'm understanding you correctly, this is what you want:

class Client(models.Model):
    account = models.ForeignKey(Account)
    email = models.EmailField()

    class Meta:
        unique_together = (('account', 'email'),)

请注意我分配了unique_together的两个元组".您可以执行一个标准的元组,但是如果有多个字段想要一起唯一,那么无论如何都只需对其进行更改.

Notice the "two-tuple" I assigned unique_together to. You can do a standard tuple, but if there is more than one field that you want to be unique together, you'll just have to change it anyway.

现在有500个不同的帐户可以有一个使用example@example.com电子邮件的客户,但是没有一个帐户可以有2个使用同一封电子邮件的客户.

Now 500 different accounts can have a client with the email example@example.com, but no account can have 2 clients with the same email.

这篇关于在多租户Django设置中处理唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆