带外键的ModelForm

时间:2014-12-01 00:06:55

标签: python django modelform

需要Django的ModelForm帮助。我想在像Blog这样的对象的末尾创建一个表单,表单" comments"在#34; Post"结束时,我尝试了几个教程,有这个例子:

class formCom(ModelForm):
class Meta : 
    model = Comentario
    exclude = ["idE"]



def poncomentario(request, pk):
p = request.POST
if 'message' in p:
    author = 'Anonymous'
    if p['author']:
        author = p["author"]
    comment = Comment(idE = Post.objects.get(pk=pk))
    cf = formCom(p, instance = comentario)
    cf.fields["author"].required = False
    comment = cf.save(commit = False)
    comment.author = author
    comment.save()
return HttpResponseRedirect(reverse("bloging.views.post", args=[pk]))

我尝试过这样的,但是我的模特" Evolucion"有20个字段:

def evo_crear(request,reg):
if request.method == 'POST':    
    evol = Evolucion(idhisto = HistoriaClinica.objects.get(pk=reg))
    form = EvoForm(request.POST, instance = evol)
    evol = form.save(commit = False)
    evol.save()
else:
    form = EvoForm()
return HttpResponseRedirect(reverse("Historias.views.detalle", args=[reg]))

在forms.py我有:

from django import forms
from Historias.models import *
class HistoForm(forms.ModelForm):
    class Meta:
model = HistoriaClinica
class EvoForm(forms.ModelForm):
    class Meta:
        model = Evolucion
    exclude = ["idhisto"]

在模板:

 <form action="" method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Save" />

但我的表单永远不会只出现&#34;输入&#34;。我究竟做错了什么? python 3.4和Django 1.7

0 个答案:

没有答案