Save the id of one model as a foreign key of another

2

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

    
asked by Ismael Caslop 24.08.2017 в 07:28
source

1 answer

0

When I commented that you added the view and the form, I did it in order to be more specific, that is, I do not know if you have control over what you are sending to the server, in your HTML form, and what you expect to receive in the server. However the problem that I see, and can provide a solution to what you need is the following:

In your view:

...
if request.method == 'POST':
    form = VacacionesForm(data=request.POST)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.persona = persona
        instance.save()
...

Not being so specific and I have to be guided by the images, I suppose that will help. In any case, I recommend that you read well that brings request.POST so you know you are sending from the HTML form and help you clarify your problem

    
answered by 25.08.2017 в 17:47