我遇到的大多数与unique_together
有关的问题都与覆盖信息有关。
我正在上传excel文件并将数据导入数据库。 我想在导入的数据重复时向用户显示错误消息。
即使它在控制台上显示,我也无法找到在模板上显示该消息的方法。
models.py
class UserData(models.Model):
GENDER_CHOICES = (
('Male', 'Male'),
('Female', 'Female'),
)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
age = models.IntegerField()
gender = models.CharField(default='Male', choices=GENDER_CHOICES, max_length=6)
address = models.CharField(max_length=200)
class Meta:
verbose_name_plural = 'User Data'
unique_together = (('first_name', 'last_name', 'age', 'gender', 'address',),)
def __str__(self):
return self.fullname()
def fullname(self):
return self.first_name + self.last_name
视图
def save_to_db(request):
if request.method == "POST":
form = Importer(request.POST, request.FILES)
if form.is_valid():
file = request.FILES['file']
import_data.delay(file)
file_name = request.FILES['file'].name
new_log = Log(action='file_upload', message=file_name)
new_log.save()
return HttpResponseRedirect(reverse('app:list'))
else:
return HttpResponseBadRequest()
else:
form = Importer()
return render_to_response('Exstore/upload_form.html', {'form': form}, context_instance=RequestContext(request))
PS:自定义错误消息不是问题,因为有this
等答案