我有一个模型菜单:
class Menu(models.Model):
loja = models.ForeignKey(Loja, related_name='menus')
nome = models.CharField(max_length=30)
ordenacao = models.IntegerField(blank=True, null=True)
class Meta:
ordering = ('ordenacao',)
#prevent equally named menus within a store(loja)
unique_together = ('loja', 'nome')
def __unicode__(self):
return self.nome
菜单表单:
class MenuForm(ModelForm):
class Meta:
model = Menu
exclude =('loja', 'ordenacao',)
添加菜单视图:
def addmenu(request, s_loja):
loja = get_object_or_404(Loja, slug=s_loja)
if not loja.activo:
return render_to_response('painelcontrolo/loja_detail.html', {
'notificacoes': ['Loja está definida como inactivo.', \
'Alterações não permitidas']})
if request.method == 'POST':
menu = Menu(loja=loja)
form = MenuForm(request.POST, instance=menu)
if form.is_valid():
menu_existe = Menu.objects.\
filter(nome=form.cleaned_data['nome']).\
filter(loja=loja)
if menu_existe:
return render_to_response(\
'painelcontrolo/loja_detail.html', {
'notificacoes': ['Já existe um menu com esse nome']
})
form.save()
return render_to_response(\
'painelcontrolo/loja_detail.html', {
'notificacoes': ['Menu adicionado']
})
else:
form = MenuForm()
return render_to_response('form_as_p.html', {
'form': form
})
错误:(在addmenu视图上添加验证后不再出现)
Request Method: POST
Request URL: http://127.0.0.1:8000/painel/bispos/addmenu/
Exception Type: IntegrityError
Exception Value: columns loja_id, nome are not unique
问题: 表单有效,但如果db上已存在nome + loja_id,则表示该模型不存在。我是否需要在其他地方添加此验证?在哪里?
编辑: 我在视图上写了一个验证,它将通知传递给模板,这很好,但并不完美。我想用用户输入重新显示表单,以便有机会在不丢失该信息的情况下修复错误。有没有办法做到这一点?
答案 0 :(得分:2)
我只是将loja
留在表单中,但将其设为隐藏字段。
class MenuForm(ModelForm):
loja = models.ModelChoiceField(Loja.objects.all(), widget=forms.HiddenInput)
class Meta:
model = Menu
exclude =('ordenacao',)
您可能需要更改视图才能调用getloja()
,无论请求是帖子还是获取。你永远不解释如何getloja()
决定什么是正确的实例......
@login_required
def addmenu(request, s_loja):
if request.method == 'POST':
form = MenuForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/painel/profile/')
else:
loja = getloja(request, s_loja) #simply retrieves the correct loja instance
menu = Menu(loja=loja)
form = MenuForm(instance=menu)
return render_to_response('form_as_p.html', {
'form': form,})
答案 1 :(得分:1)
好的,这是我能想到的最好的。它给出了表单上的错误,似乎工作正常。
@login_required
def addmenu(request, s_loja):
loja = get_object_or_404(Loja, slug=s_loja)
if not loja.activo:
return render_to_response('painelcontrolo/loja_detail.html', {
'notificacoes': ['Loja está definida como inactivo.', \
'Alterações não permitidas']})
if request.method == 'POST':
menu = Menu(loja=loja)
form = MenuForm(request.POST, instance=menu)
if form.is_valid():
menu_existe = Menu.objects.\
filter(nome=form.cleaned_data['nome']).\
filter(loja=loja)
if not menu_existe:
form.save()
return render_to_response('painelcontrolo/loja_detail.html', {
'notificacoes': ['Menu adicionado']
})
else:
form._errors['nome'] = ErrorList(\
['Um menu com esse nome já existe'])
else:
form = MenuForm()
return render_to_response('form_as_p.html', {
'form': form,
})
答案 2 :(得分:0)
我不确定表单如何知道数据库中存在哪些唯一值,它只是验证填充模型所需的字段类型,据我所知。
换句话说,“form.is_valid()”不会保存到数据库,这是您可以查看保存是否有效的唯一方法。
你必须自己抓住这个,我很确定,但“unique_together”已经在做它的工作。