I am doing CRUD using serializers and foreign key as tasked,the problem is that when I am trying to delete a data,an error which is completely unexpected has come.
this error should not be coming as I am not missing the id in the below functions and html code
NOTE : I am doing soft delete hence the parameter 'isactive' is there
delete function
def delete(request,id):
deleteclothes = Products.objects.all(id=id)
delclothes = {}
delclothes['isactive']=False
form = POLLSerializer(deleteclothes,data=delclothes)
if form.is_valid():
print("error of form when valid:",form.errors)
form.save()
return redirect('polls:show')
else:
print("error of form when not valid:",form.errors)
return redirect('polls:show')
html code of product_list
<td>
<a href="/delete/{{result.id}}/" onclick="return confirm('Are You Sure you want to delete?')">
<button >
Delete
</button>
</a>
</td>
models
class Products(models.Model):
categories = models.ForeignKey(Categories,on_delete=models.CASCADE)
sub_categories = models.ForeignKey(SUBCategories,on_delete=models.CASCADE)
color = models.ForeignKey(Colors,on_delete=models.CASCADE)
size = models.ForeignKey(Size,on_delete=models.CASCADE)
# image = models.ImageField(upload_to = 'media/',width_field=None,height_field=None,null=True)
title = models.CharField(max_length=50)
price = models.CharField(max_length=10)
sku_number = models.CharField(max_length=10)
product_details = models.CharField(max_length=300)
quantity = models.IntegerField(default=0)
isactive = models.BooleanField(default=True)
where am I going wrong in the code?
CodePudding user response:
You can't do deleteclothes = Products.objects.all(id=id)
, whether you retrieve all Products
by doing :
deleteclothes = Products.objects.all()
Or you retrieve the one with the id you want (which is what you need here) with :
deleteclothes = Products.objects.get(id=id)