将参数传递给定制的save()方法 [英] passing an argument to a custom save() method

查看:126
本文介绍了将参数传递给定制的save()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将参数传递给我的自定义保存方法,保留适当的 * args ** kwargs 传递给超级方法?我正在尝试如下:

  form.save(my_value)

  def save(self,my_value = None,* args,** kwargs):

super(MyModel,self).save(* args,** kwargs)

打印my_value

但这似乎不起作用。我做错了什么?



编辑:我发现这个例子(参见最后一条消息,传递'reorder'):
http://groups.google.com/group/django-users/ browse_thread / thread / b285698ea3cabfc9 / 6ce8a4517875cb40?lnk = raot



这本质上是我想要做的,但是 my_value 由于某种原因被认为是一个意想不到的参数。

解决方案

关键字参数必须遵循位置参数。尝试这样做:

  def save(self,my_value,* args,** kwargs):
...

或:

 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ p> 

How do I pass an argument to my custom save method, preserving proper *args, **kwargs for passing to te super method? I was trying something like:

form.save(my_value)

and

def save(self, my_value=None, *args, **kwargs):

   super(MyModel, self).save(*args, **kwargs)

   print my_value

But this doesn't seem to work. What am I doing wrong?

Edit: I found this example (see the last message, for passing 'reorder'): http://groups.google.com/group/django-users/browse_thread/thread/b285698ea3cabfc9/6ce8a4517875cb40?lnk=raot

This is essentially what I am trying to do, but my_value is said to be an unexpected argument for some reason.

解决方案

Keyword arguments must follow the positional arguments. Try this instead:

def save(self, my_value, *args, **kwargs):
    ....

or:

def save(self, *args, **kwargs):
    my_value = kwargs.pop('my_value', None)

这篇关于将参数传递给定制的save()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆