Reputation: 6555
My model does not cascade delete?
I have a FK in a model called below called 'link' upon deleting product link does not get deleted.
class Product(models.Model):
name = models.CharField(max_length=80, blank=True)
objects = ContentManager()
#FK
link = models.ForeignKey(Link, related_name="content", null=True, blank=True)
When I delete product FK remains, why?
Delete Code:
@login_required
def delete_product(request):
if request.POST:
product = get_object_or_404(Product, pk=request.POST['product_id'])
product.delete()
Link Model:
class Link(models.Model):
link = models.URLField()
hits = models.IntegerField(default=0)
Upvotes: 0
Views: 5748
Reputation: 2244
Your foreignkey is in the wrong model and should be in the Link model.
I suspect that if you delete a link instance right now it will delete any associated products
Upvotes: 3
Reputation: 706
.delete
method is not invoked when delete in bulk; as Queryset generate the sql statement and execute it directly. So deletion of ForeignKey field not invoked. If you do that, you may need to use pre_delete
and/or post_delete
signals.
https://docs.djangoproject.com/en/1.4/topics/db/queries/#topics-db-queries-delete
Upvotes: 0