Hey all,
I'm quite stumped with a problem that recently started showing up in my
code. I have a model called a Widget that can have an associated Blob
object. When a Widget is deleted, I want to make sure that the Blob
associated with it isn't used elsewhere, and if it's not, delete it. I do
that via a postDelete hook, as follows:
def postDeleteWidget(sender, **kwargs):
"""
Delete the associated blob when the widgetis deleted.
"""
widget = kwargs['instance']
#Make sure this blob isn't used elsewhere
if widget.blob is not None and Widget.objects.filter(blob=widget.blob).
count() == 0:
widget.blob.delete()
This come is failing when it first looks up widget.blob in these Django
files:
File "C:\Python27\Lib\site-packages\django\db\models\fields\related.py",
line 617, in __get__
rel_obj = qs.get()
File "C:\Python27\Lib\site-packages\django\db\models\query.py", line 334, in
get
self.model._meta.object_name
myproject.apps.blobs.models.DoesNotExist: Blob matching query does not exist
.
Now here's the weird part. If I insert "sleep(1)" after getting the widget,
the code works fine. It deletes the associated blob as expected. I'm really
at a loss in terms of how to explain this. I'd appreciate any advice. Even
printing 'widget.blob' before the if statement usually resolves it. Is this
some kind of race condition involved in accessing a related key on a
postDelete hook.
Thanks,
-Keilan
--
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 [email protected].
To post to this group, send email to [email protected].
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/75b34cd3-a987-473a-a5bd-69346fac4096%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.