I'm trying to add custom Manager to the Post model and getting the next traceback:
\Traceback (most recent call last):
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/db/models/options.py", line 654, in get_field
return self._forward_fields_map[field_name]
KeyError: 'published'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/alex/Code/django_4_by_example/blog/mysite/manage.py", line 22, in <module>
main()
File "/home/alex/Code/django_4_by_example/blog/mysite/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/core/management/__init__.py", line 420, in execute
django.setup()
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/apps/registry.py", line 116, in populate
app_config.import_models()
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/apps/config.py", line 269, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/alex/Code/django_4_by_example/blog/mysite/blog/models.py", line 12, in <module>
class Post(models.Model):
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/db/models/base.py", line 363, in __new__
new_class._prepare()
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/db/models/base.py", line 424, in _prepare
index.set_name_with_model(cls)
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/db/models/indexes.py", line 165, in set_name_with_model
column_names = [
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/db/models/indexes.py", line 166, in <listcomp>
model._meta.get_field(field_name).column
File "/home/alex/Code/django_4_by_example/blog/env/lib/python3.10/site-packages/django/db/models/options.py", line 659, in get_field
raise FieldDoesNotExist(
django.core.exceptions.FieldDoesNotExist: Post has no field named 'published'. The app cache isn't ready yet, so if this is an auto-created related field, it won't be available yet.
This is my models.py file:
from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone
class PublishedManager(models.Manager):
def get_queryset(self):
return super().get_queryset()\
.filter(status=Post.Status.PUBLISHED)
class Post(models.Model):
class Status(models.TextChoices):
DRAFT = 'DF', 'Draft'
PUBLISHED = 'PB', 'Published'
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250)
body = models.TextField()
published = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=2,
choices=Status.choices,
default=Status.DRAFT)
author = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name="blog_posts")
objects = models.Manager()
published = PublishedManager()
class Meta:
ordering = ['-published']
indexes = [
models.Index(fields=['-published']),
]
def __str__(self):
return self.title
I've changed the status of one of my posts to 'published' in admin interface, so I have no idea why it is not working. The code is exactly as in the book.
I'm trying to add custom Manager to the Post model and getting the next traceback:
I've changed the status of one of my posts to 'published' in admin interface, so I have no idea why it is not working. The code is exactly as in the book.