我想知道如何更新由User(hunter)创建的对象。这是以下产品的型号:
class Product(models.Model):
title = models.CharField(max_length=250)
pub_date = models.DateTimeField()
body = models.TextField()
image = models.ImageField(upload_to='images/')
icon = models.ImageField(upload_to='images/')
url = models.TextField()
hunter = models.ForeignKey(User, on_delete=models.CASCADE)
以下是用于创建对象的创建方法:
def create(request):
if request.method == 'POST':
if request.POST['title'] and request.POST['body'] and request.POST['url'] and request.FILES['icon'] and request.FILES['image']:
product = Product()
product.title = request.POST['title']
product.body = request.POST['body']
if request.POST['url'].startswith('http://') or request.POST['url'].startswith('https://'):
product.url = request.POST['url']
else:
product.url = 'http://' + request.POST['url']
product.icon = request.FILES['icon']
product.image = request.FILES['image']
product.pub_date = timezone.datetime.now()
product.hunter = request.user
product.save()
return redirect('/products/' + str(product.id))
能否请您帮我创建一个更新方法,以便只有创建此产品的用户才能删除它?