> Den 28/07/2015 kl. 14.36 skrev Stefan Schindler <[email protected]>:
> 
>> This makes no sense to me. You want to delete an Item or Order but 
>> then immediately create a new one?
> 
> My actual goal is this: Whenever an Item object itself is deleted, I
> want to create a LogEntry object attached to the item's order. If an
> order is deleted however, I don't want to do anything.
> 
> In code, I expected it to look something like this:
> 
>  @receiver(post_delete, sender=Item)
>  def on_item_post_delete(instance, **kwargs):
>    if instance.order is not None:
>      LogEntry(order=instance.order).save()
> 
> It's technically impossible (AFAIK) however to determine the case
> between "Item alone is deleted" and "Item is cascaded by Order
> deletion", at the moment.

Signals are nice, but sometimes they just make code more complicated. You could 
go for this (naive) approach:


class Order(models.Model):
    def delete(self, *args, **kwargs):
        # Detatch this order from its' items
        for item in self.items.all():
            item.order = None
            item.save()
            # Or is this what you really wanted?
            # item.delete()  
    super().delete(*args, **kwargs)


class Item(models.Model):
    def delete(self, *args, **kwargs):
    if self.order:
        LogEntry(order=instance.order).save()
    super().delete(*args, **kwargs)


Erik

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7983ADA9-024A-4DEC-94B1-EC2D7FEDD7DA%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to