私はアプリケーション内で、私は会社内でアクセストークンを共有しようとしています。例:地元のオフィスは、本社のトークンを使用してFacebookページに何かを投稿することができます。 FacebookやTwitterなどのソーシャルメディアサイトは、その私が作った独自の方法と抽象クラスAccessTokenにアクセストークンを扱うので、Django:GenericForeignKeyとunique_together
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
。各サイトは独自のクラスを取得します。
class FacebookAccessToken(AccessToken):
# class stuff
は、いくつかの読み取りを行った後、私はAccessToken
を継承するクラスを指すようにGenericForeignKey
を使用しなければならないことが分かりました。私は次のエラーを取得するジャンゴテストサーバーで実行すると
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'))
:私はこのエラーを取得する理由
core.ShareAccessToken: (models.E016) 'unique_together' refers to field 'contentObject' which is not local to model 'ShareAccessToken'. HINT: This issue may be caused by multi-table inheritance.
私は理解していないがGenericForeignKey
を使用して、初めて私は、次のクラスを作りました。私は間違って何をしていますか?
アクセストークンをよりスマートに共有する方法があれば、それについて聞きたいと思います。
ありがとうございました。 – CyberFly
あなたは 'contentId'ではなく' objectId'を意味すると思います。 –