2017-08-11 50 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