Search code examples
pythondjangodjango-modelsdjango-views

AttributeError: type object 'Task' has no attribute 'models'


It says my object Task has no attribute models, I tried to check if i had anywhere bad case but I don't think i have. views.py:

from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from .forms import CreateTask
from .models import Task


def Home(request):
    return render(request, "Home.html")

def task_view(request):
    current_user = request.user.id

    task = Task.models.all.filter(user=current_user)
    context = {"task": task}
    
    return render(request, "task_view.html")

def task_creation(request):
    form = CreateTask()
    if request.method == "POST":
        form = CreateTask(request.POST)

        if form.is_valid():

            task = form.save(commit=False)
            task.user = request.user

            task.save()

            return redirect("/Home/task_view/")
    
    context = {"form":form}

    return render(request, "task_creation.html")

models.py:

from django.db import models
from django.contrib.auth.models import User
import datetime

class Task(models.Model):
    title = models.CharField(max_length=100, null=True)
    description = models.CharField(max_length=5000, null=True, blank=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)

error block:

Django version 5.0.2, using settings 'task_manager.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Internal Server Error: /Home/task_view/
Traceback (most recent call last):
  File "C:\Users\lubos\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\lubos\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\lubos\OneDrive\Plocha\task_manager\tasks\views.py", line 16, in task_view
    task = Task.models.all.filter(user=current_user)
           ^^^^^^^^^^^
AttributeError: type object 'Task' has no attribute 'models'
[23/Apr/2024 16:59:12] "GET /Home/task_view/ HTTP/1.1" 500 63463

I tried checking if i misstyped anywhere but I don't think I did. Everything is in the same file.
Tutorial I went trough (didn't follow it altrough, since i had my own code, but i think i have everything same that should be):
https://www.youtube.com/watch?v=Clf60pllEN8&t=490s


Solution

  • The manager that all models (without an explicit) manager have is .objects, not .models, since you don't retrieve a collection of models, but of objects of these models.

    Furthermore .all() [Django-doc] is a method, not a property of a manager, and in fact not necessary here.

    Finally, you forgot to pass the context to the render engine of the template, so:

    We thus can work with:

    def task_view(request):
        task = Task.objects.filter(user=request.user)
        return render(request, 'task_view.html', {'task': task})