2014-03-31 2 views
0

세트를 (각 세트에 FK가 있음) 형식으로 직접 만듭니다. 내가 겪고있는 문제는 견해와 관련이 있습니다.여러 가지를 반복해서 저장하는 방법 (자동 저장)

은 내가 모든 이상 및 AJAX (같은 자동 저장의 종류)을 사용하여 이상을 업데이트 한 후것들에 대한을 설정하고 만들려고합니다. 내 경우에는 세트가 SurveySet이고 그 것이 Survey입니다.

def screen_many(request): 

    if not request.is_ajax(): 

     # get an ordered QuerySet of students 
     students = ids_to_students(request.GET.items()) 
     e_students = ids_to_students(request.GET.items(), 'e') 
     i_students = ids_to_students(request.GET.items(), 'i') 
     survey_count = len(students) 

     # Build a dataset of students with there associated behavior types. 
     data = [{'student':s.pk, 'behavior_type': 'E'} for s in e_students] 
     data += [{'student':s.pk, 'behavior_type': 'I'} for s in i_students] 

     # Use that dataset as initial data for a formset 
     SurveyFormset = formset_factory(SurveyForm, extra=0) 
     survey_formset = SurveyFormset(initial=data) 

     # ... not shown: customizing the crispy form helper 

     # Make a new survey set... 
     ss = SurveySet() 
     ss.user=request.user 
     ss.save() 

    if request.is_ajax(): 
     surveyset = get_object_or_404(SurveySet, pk=ss.pk) 
     surveys = surveyset.survey_set.all() 

     survey_formset = SurveyFormset(request.POST, instance=surveyset) 
     if survey_formset.is_valid(): 
      # Create surveys for this surveyset 
      for form in survey_formset.forms: 
       saved = form.save(commit=False) 
       saved.surveyset = ss 
       saved.save() 

     HttpResponse('saved.') 


    formsetWithStudents = zip(survey_formset.forms, students) 

    c = { 
     'formsetWithStudents' : formsetWithStudents, 
     'students' : students, 
     'survey_count' : survey_count, 
     'e_students': e_students, 
     'i_students': i_students, 
     'survey_formset': survey_formset, 
     } 
    c.update(csrf(request)) 

    return render_to_response("reports/screen_many.html", c) 

내 URL은 다음과 같습니다 경우 http://127.0.0.1:8000/screen_many/?e_1=13&e_2=12&i_1=14 뷰는 3 설문 조사 난 그냥에 대한 별도의보기를해야처럼 내가 느끼는

UnboundLocalError at /screen_many/ local variable 'ss' referenced before assignment

이 불평 모든 동안을 설정한다 ajax와 나는 SurveySet 객체가 한 번만 생성되기를 원한다.

즉, 즉. 나는 "다음 양식보기"를 클릭 한 후 업데이트되는 formset 양식을 채우고 있습니다. 이것은 내 서식 파일에 있습니다.

$('.next').click(function(){ 
     $(this).parent().hide() 
     $(this).parent().next().show() 

     var posting = $.post('/screen_many/', $('form').serializeArray()); 

     posting.done(function(response){ 
      console.log(response) 
    }); 

아니면 내가 여기에 POST 데이터를 보낼 수 있습니다 :

def save_as_you_go(request): 
    if request.is_ajax(): 
     # Get the surveyset from POST 
     ss = request.POST['form-0-surveyset'] 
     surveyset = get_object_or_404(SurveySet, pk=ss) 
     surveys = surveyset.survey_set.all() 
     SurveyFormSet = inlineformset_factory(SurveySet, Survey, form=SurveyForm, can_delete=False, extra=0) 
     survey_formset = SurveyFormSet(request.POST, instance=surveyset) 

     if survey_formset.is_valid(): 
      for form in survey_formset.forms: 
       student = form.save(commit=False) 
       student.surveyset = surveyset 
       student.save() 
     return HttpResponse('saved.') 
    else: 
     return HttpResponseRedirect('/') 

하지만 내 대답은 순진한 것 같으면

[u'ManagementForm data is missing or has been tampered with'] 

답변

0

가 용서받을 - 난 파이썬과 장고에 새로운 오전 ,하지만 ajax가 아닌 요청에서 ss 변수를 설정 한 다음 ajax 요청에서이를 참조하는 것처럼 보입니다. if 문보다 먼저 ss를 설정할 수 있습니까?

+0

이렇게하면 오류가 사라지지만 세트의 내용을 업데이트하려고 할 때마다 매번 똑같은 것으로 가득 찬 완전히 새로운 세트가 만들어집니다. 내 URL이 다음과 같은 경우 : http://127.0.0.1:8000/screen_many/?e_1=13&e_2=12&i_1=14 AJAX에서 동일한보기를 사용하기 때문에보기에서 3 개의 설문 조사 세트를 만듭니다. – broinjc

관련 문제