I believe that delete() only deletes the reference to the object in the
database, but does not affect the calling object in memory.

I think the reason for this is so that you can perform a delete operation,
and if successful,  gather information about the deleted object to display
a confirmation message to the user (which may be computationally expensive,
so you wouldn't want to run it unless the operation was successful). You
would need to reload the parent object again for the deletion to be
effective from a Python/Django perspective, similar to what you do at the
end of your test code.

-James
On Dec 19, 2015 3:06 PM, "Evan Heidtmann" <evan.heidtm...@gmail.com> wrote:

> My question is very similar to one by Seth Gordon in 2011. But it's been 4
> years since then, so I'll try again.
>
> I have models A and B, and B has a O2O field pointing to A. When I do
> `a.b.delete()`, then I'm surprised to find that `a.b` is still a valid
> object. How can I modify `a` so that it returns to its expected state (i.e.
> `a.b` throws B.DoesNotExist)?
>
> Repro app & project here:
>
> https://github.com/ezheidtmann/o2o_delete_confusion
>
> Models in question:
>
> https://github.com/ezheidtmann/o2o_delete_confusion/blob/master/maybebug/models.py
>
> Tests showing what I've tried:
>
> https://github.com/ezheidtmann/o2o_delete_confusion/blob/master/maybebug/tests.py
>
> You can see I've tried reloading the entire model from the database -- it
> works, but I am looking for something that takes less time and has fewer
> side-effects. Is there some way to reset the local cache in the Thing
> object?
>
> Thanks a lot!
>
> Evan
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAOwivZTuFJ92G_BoFPSZTq_jZoeeeXoEC-uWGu1fnYXMt37Cmw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAOwivZTuFJ92G_BoFPSZTq_jZoeeeXoEC-uWGu1fnYXMt37Cmw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUK744isxa%3D7eh6W0kZ_7QxUi8EjGgoNJsthWt3E1V9Lg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to