type object ‘Topic’ has no attribute ‘object’

In models.py

from django.db import models

class Topic(models.Model): # Тема, которую изучает пользовательн text = models.CharField(max_length=200) date_added = models.DateTimeField(auto_now_add=True)

`objects = models.Manager()`

`def __str__(self):`
    `# Возвращает строковое представление модели`
    `return self.text`

class Entry(models.Model): # Информация, изученная пользователем по теме topic = models.ForeignKey(Topic, on_delete=models.CASCADE) text = models.TextField() date_added = models.DateTimeField(auto_now_add=True)

`class Meta:`
    `verbose_name_plural = 'entries'`

`def __str__(self):`
    `if len(self.text) > 50:`
        `return self.text[:50] + "..."`
        `return self.text`

in views.py

from django.shortcuts import render from .models import Topic # Create your views here.

def index(request): # Домашняя страница Learning_log return render(request, 'learning_logs/index.html')

def topics(request): # Выводит список тем topics = Topic.objects.order_by('date_added') context = {'topics': topics} return render(request, 'learning_logs/topics.html', context)

def topic(request, topic_id): # Выводит одну тему и все ее записи topic = Topic.object.get(id=topic_id) entries = topic.entry_set.order_by('-date_added') context = {'topic': topic, 'entries': entries} return render(request, 'learning_logs/topic.html', context)

Here's the stacktrace in case it's needed:

[21/Jul/2021 15:12:05] "GET / HTTP/1.1" 200 396 [21/Jul/2021 15:12:06] "GET /topics HTTP/1.1" 200 352 Internal Server Error: /topics/1/ Traceback (most recent call last): File "/home/sergei/PycharmProjects/learning_log/11_env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/home/sergei/PycharmProjects/learning_log/11_env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/sergei/PycharmProjects/learning_log/learning_logs/views.py", line 17, in topic topic = Topic.object.get(id=topic_id) AttributeError: type object 'Topic' has no attribute 'object' [21/Jul/2021 15:12:09] "GET /topics/1/ HTTP/1.1" 500 65773

I've tried to fix this error by: objects = models.Manager() but it seems it doesn't work I'm following step by step Erik Metiz "Learning Python". I had checked the code a few times.. but still have no solution. Any ideas, guys. I really appreciate your help

Read more here: https://stackoverflow.com/questions/68472524/type-object-topic-has-no-attribute-object

Content Attribution

This content was originally published by Sergei Koshcheev at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: