【发布时间】:2021-03-06 15:49:38
【问题描述】:
我想创建一个具有一些功能的表单: 如果尚未使用 GTIN -> 在我的数据库中创建新产品 否则,如果 GTIN 已使用 -> 更新我的数据库中与此 GTIN 对应的 dateExpiry
我的代码如下:
form.py:
class productForm(ModelForm):
class Meta:
model = Product
fields = ['GTIN', 'name', 'price', 'expiryDate']
views.py:
def index(request):
if request.method == "POST":
form = productForm(request.POST).save()
return redirect('/cbPlus')
else :
form = productForm()
template = loader.get_template('cbPlus/index.html')
product = Product.objects.order_by('expiryDate')
context = {
'product' : product,
'form' : form
}
return HttpResponse(template.render(context, request=request))
models.py:
class Product(models.Model):
GTIN = models.BigIntegerField(unique=True)
name = models.CharField(max_length=25)
price = models.FloatField(max_length=25)
expiryDate = models.DateTimeField(max_length=25)
【问题讨论】:
标签: python django django-forms