我創建了一個將對象添加到模型的函數。它看起來像這樣:Django多對多導致錯誤的字段
def add_objects(self,obj_name_list):
for obj in obj_name_list:
o = Obj.objects.create(name=obj)
self.objs.add(o)
self.save(update_fields['objs'])
但是當我運行它,我得到以下錯誤:
ValueError: The following fields do not exist in this model or are m2m fields: objs
錯誤從save()
電話來了,但我不明白爲什麼...在你的回答中請給出詳細的解釋。謝謝!
這裏是回溯
.../models.pyc in add_objects(self, obj_name_list)
125 o = Obj.objects.create(name=obj) #create the tag
126 self.objs.add(o) #add the new tag to the foreign key
--> 127 self.save(update_fields=['objs'])
128 except TypeError:
129 raise TypeError("You can only add objects as a string or list")
.../models.pyc in save(self, *args, **kwargs)
95 if not self.pk:
96 is_created = True
---> 97 super(Model, self).save(*args, **kwargs)
98 if is_created:
99 signals.model_created.send(sender=self.__class__) #send signal if just created
/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/models/base.pyc in save(self, force_insert, force_update, using, update_fields)
523 raise ValueError("The following fields do not exist in this "
524 "model or are m2m fields: %s"
--> 525 % ', '.join(non_model_fields))
526
527 # If saving to the same database, and this model is deferred, then
這不是來自該功能。你應該發佈實際的回溯。 –
@DanielRoseman事實上,我從追蹤中得知了這一點。它從我的自定義保存到'super(...)'調用原來的保存。我會發布代碼,因爲它很可能會幫助 –