I have my models.py as follows:
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) I encounter an error when I try to save the data like this:
Article.objects.create(date=publish_date, title=article.title, content=article.text, author=article.authors, url=url, keywords_found=keywords_found) Here, keywords_found is a list of Keyword objects.
The error is:
TypeError: Model instances without primary key value are unhashable Where am I going wrong?
Django version : 1.10
2 Answers
Try saving it this way -
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) 0I'm currently facing a similar issue (still in the process of finding a solution), but I believe I can provide some insights that might be of help.
Within the Django framework, there exists a package named contenttypes under the django.contrib namespace, which could potentially be contributing to the problem you're encountering. You can find more information about this package here: Django ContentTypes. And it should be listed on settings.py INSTALLED_APPS
To delve into the issue, you could start by running the following SQL query on your database:
SELECT * FROM django_content_type WHERE id IS NULL;
Executing this query might reveal entries within the django_content_type table that lack an id value. Identifying these occurrences could potentially shed light on the underlying cause of the problem you're facing.