2012-02-28 109 views
1

我嘗試保存模型的m2m關係。這在您看到下面的模型中結束。我得到的錯誤是:Django save()error

save() got multiple values for keyword argument 'force_insert'

所以對我來說似乎force_insert既在args和kwargs通過。但是,當用pdb檢查這些值時,我得到以下結果:args =()kwargs = {'force_insert': True, 'using': 'default'}。所以force_insert不存在於args中。任何想法,爲什麼這會出錯?

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

代碼中的m2m關係究竟在哪裏?你能粘貼一個堆棧跟蹤嗎? – Jingo 2012-02-28 14:49:47

+0

可以這麼說,m2m相當遙遠。我將添加一個堆棧跟蹤! – 2012-02-28 15:15:33

回答

1

你打電話super關閉。你不應該通過self進入該方法。這已經涵蓋了super。改爲:

super(TaggedItem, self).save(*args, **kwargs) 
+0

啊!謝謝,就是這樣。我沒有爲這個模型編寫代碼,所以我認爲這是我的錯誤,我應該只是與我編寫的其他save()方法進行比較。 – 2012-02-28 15:24:48

+1

質疑一切;)。 – 2012-02-28 15:29:09