简体   繁体   English

Django:类没有“对象”成员

[英]Django: Class has no 'objects' member

models.py模型.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE) 

def __str__(self):
    return self.title

views.py视图.py

from django.shortcuts import render
from .models import Post

def meldungen(request):
context = {
    'posts': Post.objects.all()
}
return render(request, 'web/meldungen.html', context)

Top: There is the Error in line 4: *Class 'Post' has no 'objects' member - pylint(no-member).顶部:第 4 行存在错误:*Class 'Post' 没有 'objects' 成员 - pylint(no-member)。 I tried to migrate it an the problem wasn't solved.我试图迁移它,但问题没有解决。 But in the Django docs I read:但在 Django 文档中我读到:

If you don't add your own Manager, Django will add an attribute objects containing default Manager instance.如果您不添加自己的 Manager,Django 将添加一个包含默认 Manager 实例的属性对象。

( https://docs.djangoproject.com/en/3.0/ref/models/class/ ) https://docs.djangoproject.com/en/3.0/ref/models/class/

I read all Stack Overflow Posts for this problem but no solution worked.我阅读了有关此问题的所有 Stack Overflow 帖子,但没有任何解决方案。 I (I use VSC) tried installing pylint-django, reinstalled pylint and pip .... But here it looks like there isn't just an linter warning, because on the server (Debug is true) appears this error:我(我使用 VSC)尝试安装 pylint-django,重新安装 pylint 和 pip .... 但这里看起来不仅仅是一个 linter 警告,因为在服务器上(Debug 是真的)出现了这个错误:

Error during template rendering
In template C:\(...)\web\templates\web\base.html, error at line 9

int() argument must be a string, a bytes-like object or a number, not'NoneType'

In this line:在这一行: 在此处输入图片说明

If I comment the error line in the view.py file out the site works without any errors.如果我将 view.py 文件中的错误行注释掉,则该站点可以正常工作而不会出现任何错误。 Also I tried render the view with dummy-data as context which works.我还尝试使用虚拟数据作为有效的上下文渲染视图。 If I using the shell to take an look on my posts db there aren't problems:如果我使用 shell 查看我的帖子数据库,则没有问题:

Shell:贝壳:

In [1]: from web.models import Post

In [2]: Post.objects.all()
Out[3]: <QuerySet [<Post: Title 1>, <Post: Title 2>]>

FIXED: The problem was a bug at another place in my code.修正:问题是我代码中另一个地方的错误。 This was just an pylint-error.这只是一个pylint错误。

这只是一个pylint错误不用担心。

Got it!知道了! In my template was the error (post.id)在我的模板中是错误(post.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM