@TheBeardedTemplar, I'm happy this solves the problem, but its more like a workaround, I'm interested to understand how this raise condition is achieved. ;) One thing that I was thinking that can cause this raise condition is that by default Django emulates CASCADE DELETE behavior on ForeignKeys, which means that if you delete a Blob object all its widget will also get deleted. This means than when you delete a Blob with more than 1 widget, its, your post_delete method will be called for each widget, the first one can delete the Blob, and then there will be no Blob object for the rest, thus DoesNotExists error will be raised. (I'm not sure if this is what really is happening, its just a hypothesis).
@Covtushenko, yes this is a nice trick. On Friday, August 12, 2016 at 11:32:22 PM UTC+3, Constantine Covtushenko wrote: > > HI guis, > > I need to mention one point that works well for me. > If you check blob existence with: > hasattr(widget, 'blob') > then django responds False in the case Blob Does Not exist an True > otherwise. > > At the same time DB request is performed but DoesNotExist exception is not > raised. > > On Fri, Aug 12, 2016 at 11:10 PM, TheBeardedTemplar <thebeard...@gmail.com > <javascript:>> wrote: > >> >> >> On Friday, August 12, 2016 at 1:19:17 PM UTC-6, Todor Velichkov wrote: >>> >>> 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() >>> >>> >> Hello Todor, >> >> Your solution worked like a charm. It was fetching the blob object >> causing the failure, thank you very much! For the sake of completion, here >> is the code you asked for: >> >> class Widget(models.Model): >> client = models.ForeignKey(Client, related_name="layers") >> label = models.TextField() >> blob = models.ForeignKey(Blob, blank=True, null=True) >> date_created = models.DateTimeField(auto_now_add=True, blank=True, >> null=True) >> >> And the connection is made using >> signals.post_delete.connect(postDeleteWidget, sender=Widget). >> >> >> -- >> 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...@googlegroups.com <javascript:>. >> To post to this group, send email to django...@googlegroups.com >> <javascript:>. >> 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/ea379e82-a7e5-455e-9018-007af3c7c435%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/ea379e82-a7e5-455e-9018-007af3c7c435%40googlegroups.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/2b62f639-eb95-4b8e-8ce0-6936281f17ab%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.