Using ForeignKey to save in BD Django

0

I can not do the Data relation with Professor, I have to save Professor and pass his id to Foreign key Data and save, when I execute the code the id is not passed

forms.py

class ProfessorForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ProfessorForm, self).__init__(*args, **kwargs)
        self.fields['nome'].widget.attrs['placeholder'] = 'Professor'

    class Meta:
        model = Professor
        fields = '__all__'

class DataForm(forms.ModelForm):

    class Meta:
        model = Data
        fields = '__all__'

views.py

def home(request):
    context = {}
    template_name = 'envelope/cad_professor.html'
    if request.method == 'POST':
        form = ProfessorForm(request.POST)
        data = DataForm()
        if form.is_valid():
            data.form = form
            form.save()
            data.save()
            context['success'] = True
            return redirect (views.home)
    else:
       form = ProfessorForm()
    context['form'] = form
    return render(request,template_name , context)

PROBLEM SOLVED

data.save()
new_form = form.save(commit = False)
new_form.data = data
new_form.save()
    
asked by anonymous 18.05.2016 / 16:57

0 answers