save()获得了关键字参数的多个值

时间:2014-02-12 04:52:46

标签: django django-forms django-views

我正在尝试使用django表单中的save方法。我将2个其他参数传递给save - 用户ID和产品ID,但我收到以下错误:

TypeError at /kinect-xbox-360/review_submit/
save() got multiple values for keyword argument 'uid'

这是我的保存方法:

class ReviewForm(ModelForm):
        class Meta:
                model = Review        
                fields = ('review_text','tag')

        tag = forms.CharField(widget = forms.TextInput(attrs={'placeholder': 'teste'}), label='Descreva este produto em 1 palavra', help_text = 'separe as palavras por vírgulas')
        review_text = forms.CharField(widget = forms.Textarea(attrs={'cols':150,'rows':5}), label='O que você achou deste produto?', min_length=50, max_length=2000)

    def save(uid, product, self, commit=True):
            # save the response object
            response = super(ReviewForm, self).save(commit=False)
            #print(self.cleaned_data['review_text'])
            #response.survey = self.survey
            #user = uid
            tag = self.cleaned_data['tag']
            product = product
            review_text = self.cleaned_data['review_text']
            #created
            #updated 


            response.save()

            return response

这是处理POST响应的视图:

u = User.objects.get(username=reviews.user)
prod = Product.objects.get(slug=slug)
if request.method=="POST":
        form = ReviewForm(request.POST)

        if form.is_valid():
            response = form.save(uid=u.id, product=prod.id)
            msg = 'Obrigado por avaliar ' + prod.name
        else:
            msg = 'Houve algum erro no servidor :('
        return render(request, 'product_detail.html', {'prod':prod, 'reviews':reviews, 'user':u.first_name, 'msg':msg})    
    else:    
        form = ReviewForm()    
        args = {}
        args.update(csrf(request))
    return render(request, 'product_detail.html', {'prod':prod, 'reviews':reviews, 'user':u.first_name, 'form':form})   

1 个答案:

答案 0 :(得分:2)

Paulo在评论中走在正确的轨道上。 self必须是方法定义中的第一个参数。你不能随意重新排序这样的论点。