2017-01-08 57 views
1

我有我的models.py如下:Django的類型錯誤:沒有主鍵值模型實例是unhashable

class Article(models.Model): 
    date = models.DateTimeField(null=True, blank=True) 
    title = models.TextField(default=None, null=True, blank=True) 
    content = models.TextField(default=None, null=True, blank=True) 
    author = models.TextField(default=None, null=True, blank=True) 
    url = models.CharField(max_length=255, default=None, null=True, blank=True, unique=True) 


class Keyword(models.Model): 
    word = models.CharField(max_length=80) 

    def __str__(self): 
     return self.word 
    article = models.ForeignKey(Article, related_name='keywords_found', null=True, blank=True) 

我遇到一個錯誤,當我嘗試保存的數據是這樣的:

Article.objects.create(date=publish_date, title=article.title, content=article.text, author=article.authors, url=url, keywords_found=keywords_found) 

這裏,keywords_foundKeyword對象的列表。

的錯誤是:

TypeError: Model instances without primary key value are unhashable 

我要去哪裏錯了?

Django的版本:1.10

回答

2

嘗試保存這種方式 -

art = Article.objects.create(date=publish_date, 
           title=article.title, 
           content=article.text, 
           author=article.authors, 
           url=url) 

    Keyword.objects.create(word=keywords, article=art) 
相關問題