‘DeferredAttribute’对象没有属性

问题描述 投票:0回答:1

我是 Django 新手.. 我不知道为什么会出现错误

其 models.py 代码

class PublishManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(status=Post.Status.Draft)


# Create your models here.
# First Model
class Post(models.Model):
    class Status(models.TextChoices):
        Draft = 'Dr', "Draft"
        Released = 'RL', "Released"
        Rejected = 'RJ', "Rejected"

    # Managers
    objects = models.Manager()
    Publishedss = PublishManager()

    # Author
    Author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="up")
    # information
    title = models.CharField(max_length=200)
    description = models.TextField(max_length=9000)
    Slug = models.SlugField(max_length=250)
    # Timing
    published = models.DateTimeField(timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    Updated = models.DateTimeField(auto_now=True)
    # Status
    Status = models.CharField(max_length=2, choices=Status.choices, default=Status.Draft)

    # Sorting
    class Meta:
        ordering = ["-published"]
        indexes = [
            models.Index(fields=['-published'])
        ]

    def __str__(self):
        return self.title

提出错误

还有详细代码

def post_detail(request, id):
    print(id)
    posts1 = get_object_or_404(Post, id=id, status=Post.Status.Draft)

    context = {
        "Post": posts1
    }
    return render(request, "blog/detail.html", context)

/blog/post/1 处的属性错误 “DeferredAttribute”对象没有属性“Draft” 请求方式:GET 请求网址:http://127.0.0.1:8000/blog/post/1 Django 版本:5.0.4 异常类型:属性错误 异常值:
“DeferredAttribute”对象没有属性“Draft” 异常位置:D:\Python\Python\Projects\project1 日志 iews.py,第 24 行,在 post_detail 中 期间引发:blog.views.post_detail Python 可执行文件:D:\Python\Python\Projects\project1 env in\python.exe Python版本:3.11.7 Python路径:
['D:\Python\Python\Projects\project1', 'C:\Program Files\JetBrains\PyCharm ' '2023.3.5\plugins\python\helpers\pycharm', 'D:\Python\Python\Projects\project1', 'C:\msys64\ucrt64\lib\python311.zip', 'C:\msys64\ucrt64\lib\python3.11', 'C:\msys64\ucrt64\lib\python3.11\lib-dynload', 'D:\Python\Python\Projects\project1 env\lib\python3.11\site-packages'] 服务器时间:2024年4月25日星期四 08:14:02 +0000

python html django backend attributeerror
1个回答
0
投票

看起来您的模型可能正在尝试访问不存在的属性,特别是如果错误指向“DeferredAttribute”。当存在拼写错误或属性定义不正确时,通常会发生这种情况。确保所有字段名称拼写正确并且在您尝试访问它们时完全匹配。此外,Django 模型字段应以小写字母开头(例如,使用

author
而不是
Author
),这可能有助于避免此类问题。仔细检查您的模型字段以及您尝试访问它们的方式!

© www.soinside.com 2019 - 2024. All rights reserved.