我是python的新手,从python 3和django1.8开始。我们可以将字典作为参数传递,其中包含多个值,但是函数定义中的参数较少。
def save_myobj(request):
'''
'''
reqobj = dict(zip(request.POST.keys(), request.POST.values()))
my_dict = prepare_profile_dict(**reqobj)
obj = Model.objects.get(pk=1)
obj.__dict__.update(my_dict)
obj.save()
return HttpResponse("true")
这是我的请求对象:
reqobj = {
u'csrfmiddlewaretoken': u'xzlrzJkpKb0aAIvdJ7Z7aX05uhHjnwjX',
u'email': u'',
u'first_name': u'abc',
u'last_name': u'def',
u'revenue': u''
}
这里是prepare_profile_dict
def prepare_profile_dict(first_name, last_name, email):
''' '''
return {
"revenue" : get_revenue(),
"first_name":first_name,
"last_name":last_name,
"email":email
};
但是得到错误,
TypeError at /profile/save/
prepare_profile_dict() got an unexpected keyword argument 'revenue'
我不认为与模特有任何关系。
class Model(models.Model):
'''
'''
first_name = models.TextField("first_name", blank=True, null=True, db_index=True)
last_name = models.TextField("last_name", blank=True, null=True, db_index=True)
email = models.TextField("email", blank=True, null=True, db_index=True)
revenue = models.IntegerField()
答案 0 :(得分:4)
您的prepare_profile_dict
不接受revenue
参数(如错误消息中所述);仅first_name
,last_name
和email
。但是,调用函数时的dict扩展确实包含revenue
关键字。从reqobj
:
del reqobj['revenue']
或在**kwargs
的末尾添加prepare_profile_dict
参数以捕获任何额外的关键字参数(在函数内部将被忽略)。
def prepare_profile_dict(first_name, last_name, email, **kwargs):
...
(显然,csrfmiddlewaretoken
同样如此。)
<小时/> 此外,您可以使用querysets
update
方法:
def save_myobj(request):
reqobj = dict(zip(request.POST.keys(), request.POST.values()))
my_dict = prepare_profile_dict(**reqobj)
# ensure my_dict only contains relevant keys
Model.objects.get(pk=1).update(**my_dict)
# No .save method needed
return HttpResponse("true")
答案 1 :(得分:2)
使用ModelForm
:
# forms.py
from django import forms
from .models import Profile
from .somewhere import get_revenue
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = "__all__"
def clean_revenue(self):
return get_revenue()
# views.py
from .forms import ProfileForm
from .models import Profile
def save_myobj(request):
if request.method !== "POST":
return HttpResponseNotAllowed(["POST"])
profile = Profile.objects.get(pk=1)
form = ProfileForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return HttpResponse("true")
# returning form.errors might be better
# but anyway:
return HttpResponse("false")