2017-08-11 21 views
0

嘗試提交表單時出現__init__() got multiple values for keyword argument 'school'錯誤。這些爭論有些蹊蹺,但我不能完全明白。Django在表格中的位置/關鍵字參數問題

觀點:

if "AddCourse" in request.POST: #"AddCourse" is the name of the submit button in the template 
     f = CourseAddForm(request.POST, prefix='crs')#, school=this_school) #use Instance to edit previous stuff 
     g = SectionAddwCourseForm(request.POST, prefix='sctn', school=this_school) 
     if f.is_valid() and g.is_valid(): 
      new_course = f.save() 
      new_section = g.save(commit=False) 
      new_section.course = new_course 
      new_section.save() 
      g.save_m2m() 
      s=Scollection.objects.create(Name="All Standards",Active=True,course=new_course) 
     else: 
      print 'invalid' 

形式:

class SectionAddwCourseForm(forms.ModelForm): 
    class Meta: 
     model = Section 
     fields = ['Name','teacher'] 
     labels = { 
     "Name":"Section Name", 
     } 
    def __init__(self, school, *args, **kwargs): 
     super(SectionAddwCourseForm, self).__init__(*args, **kwargs) 
     print school 
     try: 
      #self.fields['standards'].queryset = self.instance.standards.all() #works to get current ass't stds 
      self.fields['teacher'].queryset = Teacher.objects.filter(school=school).order_by('LastName') 
     except: 
      print "except teacher list for this school" 
      pass 

回答

1

這是因爲當你調用

SectionAddwCourseForm(request.POST, prefix='sctn', school=this_school) 

self後request.POST將對應的SectionAddwCourseForm.__init__第一個參數,所以school。並且您傳遞另一個參數,其中包含關鍵字school,因此有多個值。 參數的順序非常重要!

+0

那麼這個問題的解決方案是什麼? – DeltaG

+1

好吧,這是把你的參數按你的功能預期的順序。所以學校第一。 'request.POST'之前的 –

+0

?那也行不通。 – DeltaG

相關問題