I need to set up one-to-one relation which must also be generic. May be you can advice me a better design. So far I came up to the following models
class Eve
Use .get()
return raise if object doesn't exist, .first()
return None if object doesn't exist.
Name events_relation
is an elegant way to differentiate event
from events
.
class Event(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class Meta:
unique_together = ('content_type', 'object_id') # Important
class Action1(models.Model):
events_relation = generic.GenericRelation(Event)
@property
def event(self):
# Return the object in exists
# else None
return self.events_relation.first()