简体   繁体   English

Django:GenericForeignKey和unique_together

[英]Django: GenericForeignKey and unique_together

In the application I'm working on I'm trying to share access tokens within a company. 在我正在处理的应用程序中,我正在尝试在公司内共享访问令牌。 Example: a local office can use the headquarter's tokens to post something on their Facebook page. 示例:当地办事处可以使用总部的代币在其Facebook页面上发布内容。

class AccessToken(models.Model):
    """Abstract class for Access tokens."""
    owner = models.ForeignKey('publish.Publisher')
    socialMediaChannel = models.IntegerField(
        choices=socialMediaChannelList, null=False, blank=False
    )
    lastUpdate = models.DateField(auto_now=True)

    class Meta:
        abstract = True

Since Facebook, Twitter and other social media sites handle access tokens in their own way I made and abstract class AccessToken. 由于Facebook,Twitter和其他社交媒体网站以自己的方式处理访问令牌,因此我制作了抽象类AccessToken。 Each site gets its own class eg 每个站点都有自己的类,例如

class FacebookAccessToken(AccessToken):
    # class stuff

After doing some reading I found out that I must use a GenericForeignKey to point to classes that inherit AccessToken . 在做了一些阅读后,我发现我必须使用GenericForeignKey指向继承AccessToken类。 I made the following class: 我做了以下课程:

class ShareAccessToken(models.Model):
    """Share access tokens with other publishers."""
    sharedWith = models.ForeignKey('publish.Publisher')
    sharedBy = models.ForeignKey(User)

    # for foreignkey to abstract model's children
    contentType = models.ForeignKey(ContentType)
    objectId = models.PositiveIntegerField()
    contentObject = GenericForeignKey('contentType', 'objectId')

    class Meta:
        unique_together = (('contentObject', 'sharedWith'))

When I run the django test server I get the following error: 当我运行django测试服务器时,我收到以下错误:

core.ShareAccessToken: (models.E016) 'unique_together' refers to field 'contentObject' which is not local to model 'ShareAccessToken'. core.ShareAccessToken :( models.E016)'unique_together'指的是字段'contentObject',它不是模型'ShareAccessToken'的本地。 HINT: This issue may be caused by multi-table inheritance. 提示:此问题可能是由多表继承引起的。

I don't understand why I get this error, first time using GenericForeignKey . 我不明白为什么我第一次使用GenericForeignKey会出现此错误。 What am I doing wrong? 我究竟做错了什么?

If there is a smarter way to share the access tokens I would love to hear about it. 如果有一种更聪明的方式来分享访问令牌,我很乐意听到它。

Your use of the generic foreign key in this situation is correct. 在这种情况下使用通用外键是正确的。

The error is coming from your unique_together declaration in your model. 错误来自您模型中的unique_together声明。 unique_together can only be used with columns that exist in the database. unique_together只能与数据库中存在的列一起使用。 Since contentObject is not a real column, Django complains about the constraint. 由于contentObject不是真正的列,Django抱怨约束。

Instead, you can do the following: 相反,您可以执行以下操作:

unique_together = (('contentType', 'contentId', 'sharedWidth'),)

This is equivalent to what you had defined in your question because contentObject is really just the combination of contentType and contentId behind the scenes. 这相当于您在问题中定义的内容,因为contentObject实际上只是幕后的contentTypecontentId的组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM