1
我的博客应用的models.py是这样的 -创建模型管理员
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager,
self).get_queryset().filter(status='published')
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(User, related_name='blog_posts')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices= STATUS_CHOICES,
default='draft')
objects = models.Manager()
published = PublishedManager()
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
,当我在shell中运行Post.published.filter(title__startswith='Who')
它给了我充分壳的错误这样的 -
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/core/management
/commands/shell.py", line 77, in handle_noargs
self.run_shell(shell=interface)
File "/usr/local/lib/python3.4/dist-packages/django/core/management
/commands/shell.py", line 65, in run_shell
raise ImportError
ImportError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
AttributeError: type object 'Post' has no attribute 'published'
与此混淆。如果还有其他方法,请帮助我。 给予bith ImportError以及AttributeError 我是否需要在shell中导入更多内容。 已经导入
from django.contrib.auth.models import User
from blog.models import Post
在我的壳
你确定你的shell在添加管理器之前没有启动?即当你重新打开你的shell时问题是否会持续存在? – SaeX
另外,请注意'class Meta'和'def __str__'的缩进。 – SaeX
谢谢,忘了重启shell。 –