我尝试保存模型的m2m关系。这最终会出现在下面看到的模型中。我得到的错误是:
save() got multiple values for keyword argument 'force_insert'
所以对我而言,似乎force_insert在args和kwargs中传递。但是,在使用pdb检查值时,我会得到以下信息:args = ()
和kwargs = {'force_insert': True, 'using': 'default'}
。所以在args中不存在force_insert。任何想法为什么会出错?
class TaggedItem(GenericTaggedItemBase, TaggedItemBase):
"""
Also caches team info when available.
"""
team = models.ForeignKey(Team, null=True)
def save(self, *args, **kwargs):
if hasattr(self.content_object, 'team'):
self.team = self.content_object.team
import pdb; pdb.set_trace()
super(TaggedItem, self).save(self, *args, **kwargs)
class Meta:
verbose_name = _("Tagged Item")
verbose_name_plural = _("Tagged Items")
编辑:正如预期的那样,如果我从kwargs中删除'force_insert',则保存确实有效。
堆栈跟踪:
D:\Stamkracht\v3\v3_env\lib\site-packages\django\core\handlers\base.py in get_response
response = callback(request, *callback_args, **callback_kwargs) ...
D:\Stamkracht\v3\backbone\utils.py in _decorated
return view_func(request, *args, **kwargs) ...
D:\Stamkracht\v3\v3_env\lib\site-packages\annoying\decorators.py in wrapper
output = function(request, *args, **kwargs) ...
D:\Stamkracht\v3\apps\blog\views.py in create
return _handle_blog_form(request, blog) ...
D:\Stamkracht\v3\apps\blog\views.py in _handle_blog_form
form.save_m2m() # Needed for tags! ...
D:\Stamkracht\v3\v3_env\lib\site-packages\django\forms\models.py in save_m2m
f.save_form_data(instance, cleaned_data[f.name]) ...
D:\Stamkracht\v3\apps\taggit\managers.py in save_form_data
getattr(instance, self.name).set(*value) ...
D:\Stamkracht\v3\apps\taggit\utils.py in inner
return func(self, *args, **kwargs) ...
D:\Stamkracht\v3\apps\taggit\managers.py in set
self.add(*tags) ...
D:\Stamkracht\v3\apps\taggit\utils.py in inner
return func(self, *args, **kwargs) ...
D:\Stamkracht\v3\apps\taggit\managers.py in add
self.through.objects.get_or_create(tag=tag, **self._lookup_kwargs()) ...
D:\Stamkracht\v3\v3_env\lib\site-packages\django\db\models\manager.py in get_or_create
return self.get_query_set().get_or_create(**kwargs) ...
D:\Stamkracht\v3\v3_env\lib\site-packages\django\db\models\query.py in get_or_create
obj.save(force_insert=True, using=self.db) ...
D:\Stamkracht\v3\apps\taggit\models.py in save
super(TaggedItem, self).save(self, *args, **kwargs) ...
答案 0 :(得分:1)
您致super
的电话已关闭。您不应该将self
传递给方法。这已经由super
涵盖了。请改用:
super(TaggedItem, self).save(*args, **kwargs)