I have a simple model with a shared foreign key:
class Generic(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
I would like to filter all records in this table with non-zero content_object , i.e. filter out all instances Genericwhose content objects no longer exist
Generic.objects.filter(~Q(content_object=None))
This does not work, giving an exception:
django.core.exceptions.FieldError: the 'content_object' field does not generate an automatic inverse relation and therefore cannot be used for the reverse request. If it's a GenericForeignKey, consider adding a GenericRelation.
Adding GenericRelationto reference content types does not matter.
Any help on how to achieve this will be appreciated, thank you very much.
EDIT: , , ( ).