【发布时间】:2019-03-28 21:16:47
【问题描述】:
我在 DJANGO 中创建了一个模型表单,但在插入值以填充模型时,我的 html 文件中出现错误 Coupons with this Valid coupons already exists.。
我有两个模型:
class Coupons(models.Model):
valid_coupons = models.CharField(max_length=5,unique=True)
def __str__(self):
return self.valid_coupons
class ChapterParticipated(models.Model):
code = models.ForeignKey(Coupons,unique=True,on_delete=models.PROTECT)
ChapterName = models.CharField(max_length=264)
TeamName = models.CharField(max_length=264)
def __str__(self):
return self.code
我的 view.py 包含:
from django.shortcuts import render
from first_app.forms import NewUserForm
# Create your views here.
def index(request):
form = NewUserForm()
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
else:
print('ERROR FORM INVALID')
return render(request,'firstapp/firstapp.html',{'form':form})
我的 forms.py 包含:
from django import forms
from first_app.models import Coupons
class NewUserForm(forms.ModelForm):
class Meta():
model = Coupons
fields = '__all__'
当我在输入字段中输入值并单击提交按钮时会发生什么
带有此有效优惠券的优惠券已存在。
并将输入添加到表中,但仍然显示该错误。
【问题讨论】:
标签: python html django django-models