python 字典作为参数,在字典中有多个值

python dictionary as a parameter with multiple values in a dictionary

我是 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()

您的 prepare_profile_dict 不接受 revenue 参数(如错误消息中所述);只有 first_namelast_nameemail。但是,调用函数时的字典扩展确实包含 revenue 关键字。从 reqobj:

中删除它
del reqobj['revenue']

或在 prepare_profile_dict 末尾添加一个 **kwargs 参数以捕获任何额外的关键字参数(在函数内部将被忽略)。

def prepare_profile_dict(first_name, last_name, email, **kwargs):
    ...

(显然,csrfmiddlewaretoken也是如此。)


此外,您可以使用查询集 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")

理智的解决方案使用 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")