Class "Room" has no "objects" members 类 "Room" 没有 "objects" 成员

18 浏览
0 Comments

Class "Room" has no "objects" members 类 "Room" 没有 "objects" 成员

我正在使用Django做一个Web应用,但是我一直都在遇到这个错误。

这是我的models.py文件中创建Room类的代码。

from django.db import models
import string
import random
def generate_unique_code():
    length = 6
    while True:
        code = ''.join(random.choices(string.ascii_uppercase, k=length))
        if Room.objects.filter(code=code).count() == 0:
            break
    return code
class Room(models.Model):
    code = models.CharField(max_length=8, default="", unique=True)
    host = models.CharField(max_length=50, unique=True)
    guest_can_pause = models.BooleanField(null=False, default=False)
    votes_to_skip = models.IntegerField(null=False, default=1)
    created_at = models.DateTimeField(auto_now_add=True)

这里是我在views.py中导入room的地方,我在这里也遇到了同样的错误。

from django.shortcuts import render
from rest_framework import generics
from .serializer import RoomSerializer
from .models import Room
class RoomView(generics.CreateAPIView):
    queryset = Room.objects.all()
    serializer_class = RoomSerializer

我的代码有什么问题?

0
0 Comments

问题的出现原因是在使用Visual Studio Code时,没有安装和配置pylint-django插件,导致无法识别和检查Django框架中的特定成员。

解决方法是按照以下步骤进行操作:

1. 首先需要安装pylint-django插件,可以通过运行以下命令来安装:pip install pylint-django

2. 接下来,在Visual Studio Code中按下ctrl+shift+p,然后选择Preferences: Configure Language Specific Settings

3. 这将打开一个json文件,需要将以下代码添加到该文件中并保存:

{
    "python.linting.pylintArgs": [
        "--load-plugins=pylint_django"
    ],
    "[python]": {
    }
}

4. 重新启动Visual Studio Code,问题应该得到解决。

根据此问题的答案,上述方法可以解决Class "Room" has no "objects" members的问题。

0