I have a model that has a unique generic foreign key relationship:
class Contact(models.Model):
...
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
class Meta:
unique_together = ('content_type', 'object_id',)
meaning that a Contact
can only ever belong to one object. Usually, when I want to reverse the relationship I can do
class Person(models.Model):
...
contacts = generic.GenericRelation(Contact)
and calling person.contacts.all()
will give me all the objects. Because only one Contact
will ever be returned in my situation, is there a better way of accessing this object in reverse?
p.s. I could write person.contact.all()[0]
but there must be a cleaner approach