diefenbach / django-lfs

An online-shop based on Django
http://www.getlfs.com
BSD 3-Clause "New" or "Revised" License
622 stars 222 forks source link

Deleting static block also deletes products #212

Open emilian opened 6 years ago

emilian commented 6 years ago

I had a big surprise today where deleting a static block also removed the products referencing the block. To stop this the lfs/catalog/models.py file should use on_delete=models.SET_NULL for the static_block foreign key. Like this:

static_block = models.ForeignKey("StaticBlock", on_delete=models.SET_NULL, verbose_name=_(u"Static block"), blank=True, null=True, related_name="products")

That should solve the problem for the products.

emilian commented 6 years ago

Looks good.

Is it possible to do the same for categories as well?

emilian commented 6 years ago

Right now the code is manually setting the static_block field to null for categories.

Here's the relevant code:

https://github.com/diefenbach/django-lfs/blob/master/lfs/manage/static_blocks/views.py#L247

That code could be removed with the same setting for the category model as well.

pigletto commented 6 years ago

I've updated the Pull Request. Seems that code for setting None was created before on_delete was added to Django.

emilian commented 6 years ago

That was fast. Thanks!