Daniel Holmes
Daniel Holmes

Reputation: 2002

Refresh from DB keeps OneToOneField Relation

I have a simple model relation:

class Foo(models.Model):
    bar = models.OneToOneField(Bar)

Say I do the following:

>>> bar = Bar.objects.create()
>>> foo = Foo.objects.create(bar=bar)
>>> Foo.objects.all().delete()
>>> bar.foo is None
False

This is expected because bar is still referencing the foo object. But now when I try to get a fresh copy of bar from the DB, i.e. without the related foo, I tried:

>>> bar.refresh_from_db()
>>> bar.foo is None
False 

Why does foo not come back as None? I see that in the docs it says that only fields of the model are reloaded from the database when using refresh_from_db(). Does foo not count as a field of bar in this case?

Upvotes: 0

Views: 307

Answers (1)

Kryštof Řeháček
Kryštof Řeháček

Reputation: 2483

Which Django version are you using?

Earlier versions of Django did not clear cache when relationship objects did not change it's id's. There is their ticket https://code.djangoproject.com/ticket/29076.

It's already fixed in the new versions of Django.

Upvotes: 1

Related Questions