Search code examples
pythondjangodjango-generic-relations

Django refering to same model instances in Abstract Model


I have an abstract model from which a couple of my main models are inherited. Main difficulty in this case is that I have a need in reference to the same model, like a ForeignKey to self. I have read that the ForeignKey is not possible in abstract models and GenericForeignKey can help, however I can`t really make it work.

As I understand structure should be something like following:

class BaseModel(models.Model):
    versions = GenericRelation('self')
    date_created = models.DateTimeField(default=datetime.datetime.now)
    is_deleted = models.BooleanField(default=False)

    content_type = models.ForeignKey(ContentType, blank=True, null=True)
    object_id = models.PositiveIntegerField(blank=True, null=True)
    content_object = GenericForeignKey('content_type', 'object_id')

class FirstModel(BaseModel):
    some_fields...

class AnotherModel(BaseModel):
    another_fields...

But with this approach I get an error:

>>> item1 = FirstModel.objects.get(id=1)
>>> item2 = FirstModel.objects.get(id=2)
>>> item2.content_object = item1
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/michael/.virtualenvs/diagspecgen/lib/python3.6/site-packages/django/contrib/contenttypes/fields.py", line 245, in __set__
    ct = self.get_content_type(obj=value)
  File "/home/michael/.virtualenvs/diagspecgen/lib/python3.6/site-packages/django/contrib/contenttypes/fields.py", line 163, in get_content_type
    return ContentType.objects.db_manager(obj._state.db).get_for_model(
AttributeError: 'ReverseGenericRelatedObjectsDescriptor' object has no attribute '_state'

Is that I am trying to reach is absolutely impossible and the only solution is to explicitly create needed fields in existing models?


Solution

  • I tried to replicate your problem with ForeignKey on a abstract model, but it seems to work fine with Django version 1.11.1:

    class BaseModel(models.Model):
        other = models.ForeignKey('self', null=True, blank=True)
    
        class Meta:
            abstract = True
    
    class FirstModel(BaseModel):
        pass
    
    class AnotherModel(BaseModel):
        pass
    

    Using the models:

    >>> fm1 = FirstModel.objects.create()
    >>> fm2 = FirstModel.objects.create()
    >>>
    >>> fm1.other = fm2
    >>> fm1.save()
    

    The following assignment to other results in an error:

    >>> am = AnotherModel.objects.create()
    >>> am.other = fm1
    ValueError: Cannot assign "<FirstModel: FirstModel object>": "AnotherModel.other" must be a "AnotherModel" instance.