Some more code will be helpful, for example the model structure, how do you
define the the `ForeignKey`? How do you subscribe for the signal?
You are saying that with sleep(1) the code works fine, is this mean that
you can reproduce the problem at any time?
However test it without fetching the blob object, this would probably fix
the problem:
if widget.blob_id is not None and Widget.objects.filter(blob_id=widget.
blob_id).count() == 0:
Blob.objects.filter(id=widget.blob_id).delete()
On Friday, August 12, 2016 at 9:00:58 PM UTC+3, TheBeardedTemplar wrote:
>
> 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/cc1a144f-d1e6-49a8-8121-2ec7ec8c0af6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.