首页 > 解决方案 > Django“类‘用户’没有‘对象’成员”pylint(no-member) [30, 16]

问题描述

我是 Django 新手,views.py 有一些问题。我不知道为什么,但用户模型有错误。我尝试安装 pylintpip install pylint-django{"python.linting.pylintArgs": [ "--load-plugins=pylint_django" ],}在我的 vsCode 设置中写入。但它不起作用。谁能帮我解决这个问题?

这是我的 models.py 和 views.py 的代码

模型.py:


class User(models.Model):
    username = models.CharField(max_length=100)
    
    def __str__(self):
        return self.username

视图.py:


from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from django.shortcuts import render

from stream_chat import StreamChat

from .models import User

# this decorator marks a view as being exempt from the protection ensured by the middleware
@csrf_exempt

def init(request):
    if not request.body:
        return JsonResponse(status=200, data={'message': 'No request body'})
    body = json.loads(bytes(request.body).decode('utf-8'))

    if 'username' not in body:
        return JsonResponse(status=400, data={'message': 'Username is required to join the channel'})

    username = body['username']
    
    client = StreamChat(api_key=settings.STREAM_API_KEY, 
                        api_secret=settings.STREAM_API_SECRET)
    channel = client.channel('messaging', 'General')

    try:
        user = User.objects.get(username=username)
        token = bytes(client.create_token(
            user_id=user.username)).decode('utf-8')
        return JsonResponse(status=200, data={"username": user.username, 
                                            "token": token, 
                                            "apikey": settings.STREAM_API_KEY})
    
    except User.DoesNotExist:
        user = User(username=username)
        user.save()
        token = bytes(client.create_token(
            user_id=username)).decode('utf-8')
        client.update_users({"id": username, "role": "admin"})
        channel.add_members([username])

        return JsonResponse(status=200, data={"username": user.username, 
                                                "token": token, 
                                                "apiKey": settings.STREAM_API_KEY})

谢谢

标签: python-3.xdjangopylint

解决方案


我认为问题可能出在您的导入from .models import User应该是绝对的from mypackage.models import User.


推荐阅读