ModelForm with foreign key

129 Views Asked by At

need help at Django's ModelForm. I want to create a form at the end of an object like a Blog, the form "comments" is at the end of "Post", I tried with several tutorialstha have this example:

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]))

I tried the same like this, but my model "Evolucion" has 20 fields:

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]))

at forms.py I have :

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"]

at templates:

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

But my forms never appears only the "input". What am I doing wrong? python 3.4 and Django 1.7

0

There are 0 best solutions below