2
Calling refresh_from_db
of the related instance should do. You can make it transparent by overriding delete
of the dependent model.
class OtherModel(models.Model):
onetoone = OneToOneField(MyModel)
def delete(self, using=None, keep_parents=False):
result = self.delete(using, keep_parents)
self.onetoone.refresh_from_db()
return result
0
The hasattr
will always return True, even if you’ve never created the relationship in the first place. It is not the right thing to be using here.
Instead, you need to check whether there is a related object.
try:
obj.onetoone
except OtherModel.DoesNotExist:
print("does not exist")
- [Answered ]-How to chain a queryset together in Django correctly
- [Answered ]-Celery .env variables not used in settings.py
- [Answered ]-RemovedInDjango19Warning – isn't in an application in INSTALLED_APPS
Source:stackexchange.com