gokul dev.p
gokul dev.p

Reputation: 3

'function' object has no attribute 'objects' in django

i am getting a error: AttributeError at / 'function' object has no attribute 'objects' Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 3.2.5 Exception Type: AttributeError Exception Value:
'function' object has no attribute 'objects' Exception Location: G:\PYTHON DJANGO 2021 - COMPLETE COURSE\first_django_project\devsearch\project\views.py, line 27, in projects Python Executable: C:\ProgramData\Anaconda3\envs\djangoenv\python.exe Python Version: 3.9.6 Python Path:
['G:\PYTHON DJANGO 2021 - COMPLETE COURSE\first_django_project\devsearch', 'C:\ProgramData\Anaconda3\envs\djangoenv\python39.zip', 'C:\ProgramData\Anaconda3\envs\djangoenv\DLLs', 'C:\ProgramData\Anaconda3\envs\djangoenv\lib', 'C:\ProgramData\Anaconda3\envs\djangoenv', 'C:\ProgramData\Anaconda3\envs\djangoenv\lib\site-packages'] Server time: Sat, 21 Aug 2021 13:18:04 +0000

# models.py
from django.db import models
import uuid


# Create your models here.
class Project(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(null=True,blank=True)
    demo_link = models.CharField(max_length=2000,null=True,blank=True)
    source_link = models.CharField(max_length=2000,null=True,blank=True)
    tag = models.ManyToManyField('Tag',blank=True)
    vote_total = models.IntegerField(default=0,null=True,blank=True)
    vote_ratio = models.IntegerField(default=0,null=True,blank=True)
    created_on = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4,unique=True,primary_key=True,editable=False)
    
    def __str__(self):
        return self.title


# views.py
from django.shortcuts import render
from .models import Project


# Create your views here.
def projects(requests):
    projects = Project.objects.all()
    return render(requests,"project/projects.html", {'projects':projects})

Upvotes: 0

Views: 2387

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477220

You likely defined a function in your views named Project, as a result the reference to the Project model is altered to the view function.

You can define a view function name in lowercase, so project instead of Project, and thus implement this as:

from django.shortcuts import render
from .models import Project

# Create your views here.
def projects(requests):
    projects = Project.objects.all()
    return render(requests, 'project/projects.html', {'projects':projects})

# project instead of Project
def project(request, pk):
    # …

Upvotes: 2

David Dunne
David Dunne

Reputation: 53

try migrating the database

python manage.py makemigrations
python manage.py migrate

Upvotes: 0

Related Questions