Concadenamiento ID
Publicado por Ericj (1 intervención) el 20/09/2019 18:51:50
Estimada comunidad tengo el siguiente problema:
Tengo dos formularios los cuales están unidos por Foreignkey, los modelos son los siguientes:
y mis urls
La idea es obtener una lista de pacientes, en donde al dar click en el nombre del paciente se redirija a una pagina que nos muestre el registro de enfermedades de este paciente y poder editar, eliminar y añadir. El problema es que al llegar al registro de enfermedades pierdo el id del paciente por lo que se me hace imposible recuperarlo. Si alguien tiene la solucion se los agradeceria ya que soy nuevo en el tema y se me dificulta mucho hacerlo
Tengo dos formularios los cuales están unidos por Foreignkey, los modelos son los siguientes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Paciente(models.Model):
pac_cedula=models.CharField(max_length=10,unique=True)
pac_nombres=models.CharField(max_length=40)
pac_apellido_materno=models.CharField(max_length=30)
pac_apellido_paterno=models.CharField(max_length=30)
pac_fecha_nacimiento=models.DateField(blank=True, null=True)
pac_codigo=models.CharField(max_length=50)
residencia=models.CharField(max_length=100)
procedencia=models.CharField(max_length=200)
es_civil = (('Sol', 'Solter@'),('Cas', 'Casad@'),('Div','Divorciad@'),('Viu','Viud@'))
estado_civil=models.CharField(max_length=3, choices=es_civil)
actividad_laboral=models.CharField(max_length=200)
nivel_educacion=models.CharField(max_length=200)
tel=models.CharField(max_length=200)
edad=models.CharField(max_length=3)
gen=(('Masculino','Masculino'),('Femenino','Femenino'))
genero=models.CharField(max_length=15, choices=gen)
def __str__(self):
return "%s %s" % (self.pac_apellido_paterno, self.pac_nombres)
class Enfermedad(models.Model):
enfer=models.CharField(max_length=50)
descripcion=models.CharField(max_length=500)
paciente=models.ForeignKey(Paciente,null= True,blank=False,on_delete=models.CASCADE)
def __str__(self):
return "{0} => {1}".format(self.paciente,self.enfer)
A su vez las correspondientes vistas son las siguientes:
class PacienteCreate(CreateView):
model=Paciente
form_class=Paciente_Form
template_name='paciente/Paciente_create.html'
success_url=reverse_lazy('paciente')
class DetailView(generic.DetailView):
model=Paciente
template_name='paciente/pacientename.html'
def paciente(request,paciente_id):
return (self.paciente_id)
class EnfermedadListView(generic.ListView):
model=Enfermedad
context_object_name = 'listaenfermedad'
template_name = 'paciente/enfermedad.html'
class PacienteListView(generic.ListView):
model=Paciente
context_object_name = 'listatratamiento'
template_name = 'paciente/paciente.html'
class PacienteUpdate(UpdateView):
model=Paciente
form_class=Paciente_Form
template_name='paciente/Paciente_edit.html'
success_url=reverse_lazy('paciente')
class PacienteDelete(DeleteView):
model=Paciente
form_class=Paciente_Form
template_name='paciente/Paciente_delete.html'
success_url=reverse_lazy('paciente')
class EnfermedadCreate(CreateView):
model=Enfermedad
form_class=EnfermedadForm
template_name='paciente/enfermedad_create.html'
success_url=reverse_lazy('enfermedad')
class EnfermedadUpdate(UpdateView):
model=Enfermedad
form_class=EnfermedadForm
template_name='paciente/enfermedad_edit.html'
success_url=reverse_lazy('enfermedad')
class EnfermedadDelete(DeleteView):
model=Enfermedad
form_class=EnfermedadForm
template_name='paciente/enfermedad_delete.html'
success_url=reverse_lazy('enfermedad')
y mis urls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
urlpatterns = [
#pacientes
url(r'^registropaciente/$', PacienteCreate.as_view(), name='r-Paciente'),
url(r'^Paciente/$', PacienteListView.as_view(), name='paciente'),
url(r'^eliminar_Paciente/(?P<pk>\d+)$', PacienteDelete.as_view(), name='Paciente _delete'),
url(r'^editar_Paciente/(?P<pk>\d+)$', PacienteUpdate.as_view(), name='Paciente_edit'),
url(r'^paciente/(?P<pk>\d+)$', DetailView.as_view(), name='pacientename'),
url(r'^paciente/(?P<paciente_id>\d+)/enfermedad/$', EnfermedadListView.as_view(), name='enfermedad'),
url(r'^paciente/(\d+)/enfermedad/registroenfermedades/$', EnfermedadCreate.as_view(), name='r-enfermedad'),
url(r'^paciente/(\d+)/enfermedad/eliminar_enfermedad/(?P<pk>\d+)$', EnfermedadDelete.as_view(), name='enfermedad_delete'),
url(r'^paciente/(\d+)/enfermedad/editar_enfermedad/(?P<pk>\d+)$', EnfermedadUpdate.as_view(), name='enfermedad_edit'),
]
Valora esta pregunta


0