I have these 2 models
class Persona(models.Model):
opciones = [('masculino', 'Masculino'), ('femenino', 'Femenino')]
nombre = models.CharField(max_length=30)
apellido_paterno = models.CharField(max_length=30)
apellido_materno = models.CharField(max_length=30)
fecha_nacimiento = models.DateField(null=True, blank=True)
curp = models.CharField(max_length=18)
rfc = models.CharField(max_length=18)
sexo = models.CharField(max_length=70, choices=opciones)
class Vacaciones(models.Model):
dias_tomados = models.CharField(max_length=50)
fecha_inicio = models.DateField()
razon = models.CharField(max_length=50)
observaciones = models.CharField(max_length=50)
personas = models.ForeignKey(Persona, null=True, blank=True)
This is the form
class VacacionesForm(forms.ModelForm):
class Meta:
model = Vacaciones
fields = [
'dias_tomados',
'fecha_inicio',
'razon',
'observaciones',
]
labels = {
'dias_tomados': 'Días tomados',
'fecha_inicio': 'Fecha de inicio',
'razon': 'Razón',
'observaciones': 'Observaciones',
}
widgets = {
'dias_tomados': forms.TextInput(attrs={'class':'form-control'}),
'fecha_inicio': forms.TextInput(attrs={'class':'form-control'}),
'razon': forms.TextInput(attrs={'class':'form-control'}),
'observaciones': forms.TextInput(attrs={'class':'form-control'}),
}
And this is the view
def AsignaVacaciones(request, prs_id):
persona = Persona.objects.get(id=prs_id)
if request.method == 'POST':
form = VacacionesForm(request.POST)
if form.is_valid():
form.save()
return redirect('personas:listado_persona')
else:
form = VacacionesForm()
return render(request, 'vacaciones/vacaciones_form.html', {'vacaciones':form})
I am trying to place the id of the people who request their vacations in the field personas_id (FK) of the holiday model but until now I have not gotten it. I have been looking for how to carry out this relationship but nothing has worked for me.
My question is, is it possible to realize this relationship from the views?.
In the template it looks like this.
until this point the id of the person who wishes to request his vacation has been recovered, but I can not get the id of the person to be saved as a value of the field persons_id of the holiday model