I have the following models
class SimulationStatus(models.Model):
simulation = models.ForeignKey(Simulation, on_delete=models.CASCADE)
name = models.CharField(
max_length=80,
choices=SimStatusOptions.to_sequence()
)
# Simulation time
time = models.PositiveIntegerField(
null=True, blank=True,
verbose_name="time (in seconds)",
help_text="simulation time in seconds"
)
# Field to track the active_status
active_simulation = models.OneToOneField(
Simulation, on_delete=models.CASCADE, related_name='active_status',
null=True, blank=True
)
class Simulation(models.Model):
"""
Simulation model
"""
uuid = models.UUIDField(default=uuid.uuid4)
organisation = models.ForeignKey(
Organisation, on_delete=models.CASCADE, null=False, blank=False
)
user = models.ForeignKey(
User, on_delete=models.CASCADE, null=False, blank=False
)
The reverse lookup on the active_status
throws an exception even though the instance does exist.
from api.simulations.models import Simulation
s = Simulation.objects.get(id=852)
s.active_status
# exception
---------------------------------------------------------------------------
RelatedObjectDoesNotExist Traceback (most recent call last)
<ipython-input-11-bbd6df757cc6> in <module>
----> 1 s.active_status
/usr/local/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py in __get__(self, instance, cls)
413 "%s has no %s." % (
414 instance.__class__.__name__,
--> 415 self.related.get_accessor_name()
416 )
417 )
RelatedObjectDoesNotExist: Simulation has no active_status.
from api.simulations.statuses.models import SimulationStatus
ss = SimulationStatus.objects.get(simulation=s)
ss
<SimulationStatus: created 0 sec, simulation: weir>
I don't really understand why this is. I've read this post but my relation is not empty.