36
for item in data:
category_id = item['category_id']
del item['category_id']
category = Category.objects.get(pk=category_id)
code = item['code']
try:
article = Article.objects.get(pk=code)
except:
article = Article(**item)
article.save()
# at this point I have the article & category, but the next
# statement throws me an error:
category.articles.add(article)
category.save()
的錯誤是:AttributeError:'ManyRelatedManager'對象沒有屬性'add'?我喜歡在Django的網站,但得到這個錯誤
AttributeError: 'ManyRelatedManager' object has no attribute 'add'
您是否在使用直通模式?如果是這樣.add和.create將不起作用。 – JamesO