Anna U
Anna U

Reputation: 21

Django Error during template rendering no such column

I was trying to show the list of the User's to-do lists using view.html. The error says:

no such column:testapp_todolist.user_id".

But I don't understand where this column is and how it is related to the red line in my view.html:

{% for td in user.todolist.all %}

Can you please explain in details how do I add this column?

Here's my models.py:

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class ToDoList(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="todolist", default=0)
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class Item(models.Model):
    todolist = models.ForeignKey(ToDoList, on_delete=models.CASCADE)
    text = models.CharField(max_length=200)
    complete = models.BooleanField()
    
    def __str__(self):
        return self.text

enter image description here

Upvotes: 0

Views: 590

Answers (2)

Kaiss B.
Kaiss B.

Reputation: 327

user does not have the object todolist, todolist is a foreign key in Item. You should do:

list = ToDoList.objects.filter(user=YOUR_USER_HERE)

or

# if you want to get the todolist of the currently logged in user
list = ToDoList.objects.filter(user=request.user) 

Upvotes: 0

Ramy Berrekia
Ramy Berrekia

Reputation: 83

Try solving this by writing this on your console:

  • python manage.py makemigrations
  • python manage.py migrate

Those commands are going to create tables for the TodoList model in the database.

Upvotes: 1

Related Questions