django - Generic Foreign Key with computed content type instead of fk to content type model -




the simplest case of creating generic foreign key this:

class modelwithgfk(models.model):     content_type = models.foreignkey(contenttype, on_delete=models.cascade)     object_id = models.positiveintegerfield()     content_object = genericforeignkey('content_type', 'object_id') 

but in case there other fields wich can infer content type from, adding content_type fk both redundant , prone cause inconsistency on time. tried this:

class modelwithgfk(models.     object_id = models.positiveintegerfield()     content_object = genericforeignkey('content_type', 'object_id')      @property     def content_type(self):         # figure out wich content type instance should have fk         return contenttype.objects.get(..........) 

but seems gfk class accepts content type argument foreign key:

def _check_content_type_field(self):     """     check if field named `field_name` in model `model` exists ,     valid content_type field (is foreignkey contenttype).     """ 

is there workaround (perhaps via overriding gfk , of methods get_content_type) or shall give in, add fk , live in constant fear of inconsistency , agony of knowing (tiny) inevitable redundancy?





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -