python - Django: GenericForeignKey and unique_together -
in application i'm working on i'm trying share access tokens within company. example: local office can use headquarter's tokens post on facebook page.
class accesstoken(models.model): """abstract class 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 , other social media sites handle access tokens in own way made , abstract class accesstoken. each site gets own class e.g.
class facebookaccesstoken(accesstoken): # class stuff
after doing reading found out must use genericforeignkey
point classes inherit accesstoken
. made following class:
class shareaccesstoken(models.model): """share access tokens other publishers.""" sharedwith = models.foreignkey('publish.publisher') sharedby = models.foreignkey(user) # foreignkey abstract model's children contenttype = models.foreignkey(contenttype) objectid = models.positiveintegerfield() contentobject = genericforeignkey('contenttype', 'objectid') class meta: unique_together = (('contentobject', 'sharedwith'))
when run django test server following error:
core.shareaccesstoken: (models.e016) 'unique_together' refers field 'contentobject' not local model 'shareaccesstoken'. hint: issue may caused multi-table inheritance.
i don't understand why error, first time using genericforeignkey
. doing wrong?
if there smarter way share access tokens love hear it.
your use of generic foreign key in situation correct.
the error coming unique_together
declaration in model. unique_together
can used columns exist in database. since contentobject
not real column, django complains constraint.
instead, can following:
unique_together = (('contenttype', 'contentid', 'sharedwidth'),)
this equivalent had defined in question because contentobject
combination of contenttype
, contentid
behind scenes.
Comments
Post a Comment