3

私のモデルは.....のように見えます

Student(models.Model):
    name = models.CharField(max_length = 60, blank = False)
    r_no = models.CharField(max_length = 60, blank = False)
    created_date = models.DateTimeField(null = False, blank = False, default = datetime.datetime.now())

StudentPotential(models.Model):
    aka_name = models.CharField(max_length = 60, blank = True)
    -----
    -----
StudentCorrespondence(models.Model):
    student = models.ForeignKey('Student', related_name = "Student_FK")
    student_p = models.ForeignKey('Student', related_name = "Student_FK")
    emailed_date = models.DateTimeField(null = True, blank = True)
    phoned_date = models.DateTimeField(null = True, blank = True)

form.pyのマイフォーム

class StudentPotentialForm (forms.ModelForm):

    class Meta:
        model = StudentPotential

class StudentCorrespondenceForm(forms.ModelForm):
    class Meta:
        model = StudentCorrespondence
        exclude = ('student', 'student_p')

最後に私のview.py

def add_student_company_potential(request, student_id):
    from cdradmin.forms import StudentPotentialForm, StudentCorrespondenceForm
    if request.method == 'POST':
            ### HOW TO SAVE THE two from for the student have its it 'student_id' ####

    else:

            StudentPotentialForm = StudentPotentialForm()
            StudentCorrespondenceForm = StudentCorrespondenceForm()
context = {'StudentCorrespondenceForm':StudentCorrespondenceForm, "StudentPotentialForm":StudentPotentialForm}
return render_to_response('cdradmin/studentform.html', context, context_instance = RequestContext(request))

データがビューに投稿されたら、IDが「student_id」である学生のためにこのデータを保存するにはどうすればよいですか?

4

1 に答える 1

3

あなたはこれを試すことができます

if request.method == 'POST':
    spf = StudentPotentialForm(request.POST)
    if spf.is_valid():
        osp = spf.save()
    else :
       #raise error

    student = Student.objects.get(id=student_id)
    scf = StudentCorrespondenceForm(request.POST)
    if scf.is_valid():
        osc = scf.save(commit=False)
        osc.student = student
        osc.student_p = osp
        osc.save()
    else:
        # raise error.
于 2012-09-28T13:03:17.037 に答える