'model.objects.all() doesn't find anything when foreign key in model
when I load the website i get this error
my models.py file looks like:
# Create your models here.
class Information(models.Model):
id = models.CharField(max_length=200, primary_key=True)
title = models.CharField(max_length=500)
link = models.CharField(max_length=100)
summary = models.CharField(max_length=1000)
published = models.CharField(max_length = 100)
def __str__(self):
return self.title
class Search(models.Model):
id = models.CharField(max_length=100, primary_key=True)
searched_titles = models.CharField(max_length=100)
searched_topics = models.CharField(max_length=100)
number_found_articles = models.IntegerField()
def __str__(self):
return self.id
class Article_search(models.Model):
id = models.AutoField(primary_key=True)
found_articles = models.ForeignKey(
Information, on_delete=models.CASCADE)
search_details = models.ForeignKey(
Search, on_delete=models.CASCADE)
def __str__(self):
return self.id
in my view.py file:
def show_articles_with_this_filter_id(request):
all = Article_search.objects.all()
print(all)
return render(request, 'show_article.html')
when I get to the print statement I get the error shown in the picture: Unknown column 'database_article_search.found_articles_id' in 'field list'
why is the part _id pasted behind found_articles?
this is the error i get when i remove the id from article_search
Solution 1:[1]
# Create your models here.
class Information(models.Model):
title = models.CharField(max_length=500)
link = models.CharField(max_length=100)
summary = models.CharField(max_length=1000)
published = models.CharField(max_length = 100)
def __str__(self):
return self.title
class Search(models.Model):
searched_titles = models.CharField(max_length=100)
searched_topics = models.CharField(max_length=100)
number_found_articles = models.IntegerField()
def __str__(self):
return self.searched_titles
class Article_search(models.Model):
found_articles = models.ForeignKey(
Information, on_delete=models.CASCADE)
search_details = models.ForeignKey(
Search, on_delete=models.CASCADE)
Make model like this and then run two commands 1.python manage.py makemigrations and then 2.python manage.py migrate
"In your views.py file"
def show_articles_with_this_filter_id(request,id):
all = Article_search.objects.filter(id=id)
print(all)
return render(request, 'show_article.html')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
