I've recently upgraded one of my Django projects from 1.9.6
to 2.2
and in doing so I'm getting a strange error around a specific ForeignKey
relation.
models.py
class MyObject1(models.Model):
myobject2 = models.ForeignKey(MyObject2, on_delete = models.CASCADE)
views.py
def my_view(request, id):
try:
my_object = MyObject1.objects.get(id = id)
except:
# do some stuff
else:
print (my_object.myobject2)
result
RelatedObjectDoesNotExist
MyObject1 has no myobject2
at line print (my_object.myobject2)
I have confirmed via the Django shell that the instance in question does have a valid myobject2
and I don't get that error when performing the same actions in the shell.
All other ForeignKey
relations in the application work as expected except for this one.
This is quite puzzling and all help is appreciated. Thanks!