我收到了以下表格:
class CourseAddForm(forms.ModelForm):
"""Add a new course"""
name = forms.CharField(label=_("Course Name"), max_length=100)
description = forms.Textarea()
course_no = forms.CharField(label=_("course Number"), max_length=15)
#Attach a form helper to this class
helper = FormHelper()
helper.form_id = "addcourse"
helper.form_class = "course"
#Add in a submit and reset button
submit = Submit("Add", "Add New Record")
helper.add_input(submit)
reset = Reset("Reset", "Reset")
helper.add_input(reset)
def clean(self):
"""
Override the default clean method to check whether this course has been already inputted.
"""
cleaned_data = self.cleaned_data
name = cleaned_data.get('name')
hic = cleaned_data.get('course_no')
try:
course=Course.objects.get(name=name)
except Course.DoesNotExist:
course=None
if course:
msg = u"Course name: %s has already exist." % name
self._errors['name'] = self.error_class([msg])
del cleaned_data['name']
return cleaned_data
else:
return self.cleaned_data
class Meta:
model = Course
正如您所看到的,我覆盖了clean方法,以检查当用户尝试添加该课程时,该课程是否已存在于数据库中。这对我来说很好。
但是,当我想为表单添加相同的检查以进行编辑时,问题就出现了。因为它正在编辑,所以DB中已存在具有相同课程名称的记录。因此,相同的检查会抛出课程名称已经存在的错误。但我需要检查重复,以避免用户将课程名称更新为另一个已存在的课程名称。
我正在考虑检查课程名称的值以查看它是否已更改。如果它已被更改,我可以执行与上面相同的检查。如果没有更改,我不需要进行检查。但我不知道如何获取原始数据进行编辑。
有没有人知道如何在Django中执行此操作?
我的观点如下:
@login_required
@csrf_protect
@never_cache
@custom_permission_required('records.change_course', 'course')
def edit_course(request,course_id):
# See if the family exists:
try:
course = Course.objects.get(id=course_id)
except Course.DoesNotExist:
course = None
if course:
if request.method == 'GET':
form = CourseEditForm(instance=course)
return render_to_response('records/add.html',
{'form': form},
context_instance=RequestContext(request)
)
elif request.method == 'POST':
form = CourseEditForm(request.POST, instance=course)
if form.is_valid():
form.save()
return HttpResponseRedirect('/records/')
# form is not valid:
else:
error_message = "Please correct all values marked in red."
return render_to_response('records/edit.html',
{'form': form, 'error_message': error_message},
context_instance=RequestContext(request)
)
else:
error = "Course %s does not exist. Press the 'BACK' button on your browser." % (course)
return HttpResponseRedirect(reverse('DigitalRecords.views.error', args=(error,)))
谢谢。
答案 0 :(得分:3)
我相信从结果中排除当前实例ID可以解决问题:
from django.db.models import Q
try:
qs = Course.objects.filter(name=self.cleaned_data.get('name'))
if self.instance.pk is not None:
qs = qs.filter(~Q(pk=self.instance.pk))
course = qs.get()
except Course.DoesNotExist:
course = None
然而正如dokkaebi指出的那样,独特的确是更好的方法,因为这种解决方案容易受到竞争条件的影响。我不确定你的数据模型是什么样的,但我怀疑定义
class Meta:
unique_together = ('department', 'name')
应该完成你想要的。
答案 1 :(得分:2)
我认为您应该在unique=True
字段上设置Course.name
,让框架为您处理验证。
<强>更新
由于unique=True
不是您的案例的正确答案,您可以这样检查:
def clean(self):
"""
Override the default clean method to check whether this course has
been already inputted.
"""
cleaned_data = self.cleaned_data
name = cleaned_data.get('name')
matching_courses = Course.objects.filter(name=name)
if self.instance:
matching_courses = matching_courses.exclude(pk=self.instance.pk)
if matching_courses.exists():
msg = u"Course name: %s has already exist." % name
raise ValidationError(msg)
else:
return self.cleaned_data
class Meta:
model = Course
作为旁注,我还更改了您的自定义错误处理,以使用更标准的ValidationError
。