如何设置模型实例以在Django中生成确认表单

问题描述 投票:0回答:1

我需要用外键保存这些模型的信息,所以我为Candidato,InfoPersonal和InfoAcademica创建了一个视图,最后我创建了一个确认视图以保存Solicitud但该页面显示我:

TypeError at /solicitud/confirmacion/2/
'instance' is an invalid keyword argument for this function

我的模特。

project/apps/solicitud/models.py 

class Candidato(models.Model):
    nombre = models.CharField(max_length=50)
    apellidos = models.CharField(max_length=70)
    email = models.EmailField(unique=True)

    def __unicode__(self):
        return u'{} {}'.format(self.nombre, self.apellidos)


class InfoPersonal(models.Model):
    candidato = models.ForeignKey(Candidato, null=False, blank=False, on_delete=models.CASCADE)
    sexo = models.CharField(max_length=9, choices=SEXO_CHOICES)
    fecha_nacimiento = models.DateField()
    curp = models.CharField(max_length=18, unique=True)

    pais_origen = models.CharField(max_length=30, default="México")
    lugar_nacimiento = models.CharField(max_length=100)

    domicilio = models.CharField(max_length=120)
    codigo_postal = models.CharField(max_length=5)
    telefono = models.CharField(max_length=20)

    def __unicode__(self):
        return u'{}'.format(self.curp)


class InfoAcademica(models.Model):
    persona = models.ForeignKey(Candidato, null=True, blank=True, on_delete=models.CASCADE)
    escuela_procedencia = models.CharField(max_length=50)
    programa_solicitado = models.CharField(max_length=50, choices=PROGRAMA_SOLICITADO_CHOICES, default=MAS_ADMIN)
    titulado = models.CharField(max_length=10, choices=ESTADO_TITULACION_CHOICES, default=YA_TITULADO)
    titulacion_creditos = models.CharField(max_length=2, choices= TITULACION_CREDITOS_CHOICES, default=NO)

    def __unicode__(self):
        return u'{}'.format(self.programa_solicitado)


class Solicitud(models.Model):
    candidato = models.ForeignKey(Candidato, null=True, blank=True)
    academica = models.ForeignKey(InfoAcademica, null=False, blank=False)
    Personal = models.ForeignKey(InfoPersonal, null=False, blank=False)

    def __unicode__(self):
        return u'Solicitud id: {}'.format(self.id)

我的URLS,在这里我发送每个模型的PK以在Solicitud中链接它们

    # -*- coding: utf-8 -*-
from django.conf.urls import url
import views

app_name = 'solicitud'

urlpatterns = [
    url(r'^datos_candidato/$', views.AddDatosCandidato.as_view(), name='datos_candidato'),
    url(r'^datos_personales/$', views.AddDatosPersonales.as_view(), name='datos_personales'),
    url(r'^datos_academicos/$', views.AddDatosAcademicos.as_view(), name='datos_academicos'),
    url(r'^confirmacion/(?P<pk>\d+)/$', views.AddSolicitud.as_view(), name='datos_confirmacion'),
]

最后我的观点,在这里我不知道如何发送2个模型的instancen并将其保存在Solicitud中

project/apps/solicitud/views.py

class AddSolicitud(CreateView):
    model = Solicitud, InfoPersonal, InfoAcademica
    form_class = Solicitud
    template_name = 'solicitud/confirmacion_solicitud.html'
django django-models django-templates django-views
1个回答
0
投票

你有form_class = Solicitud,但Solicitud是一个模型而不是形式。

此外,您不能在model =行中指定多个模型。

© www.soinside.com 2019 - 2024. All rights reserved.