Suppose I have these models:
class Space(models.Model):
item = models.OneToOneField(Item, null=True, on_delete=models.CASCADE)
class Item(models.model):
...
A Space can contain an Item, but it can be empty too. On the other hand, every Item must have a Space. It cannot exist in a void.
I could create a field .space in my Item model (with null=False), but then Space.container can never be empty (raising a RelatedObjectDoesNotExist
exception when creating/querying them).
Handling these exceptions is cumbersome. I'd rather have Space.container return None (as is the case with my current setup).
Is there an easy way to make .space in the Item model mandatory, but not the other way around?