python-3.x - 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})
谢谢
解决方案
我认为问题可能出在您的导入from .models import User
应该是绝对的from mypackage.models import User
.
推荐阅读
- dll - 是否可以在最新版本的 ASP.NET Core 中添加 COM 引用?
- python-3.x - 有没有办法生成一个具有索引替代情况的字符串?
- delphi - 如何在 Delphi 中制作关于带有两个单选按钮的单选组的 if 语句
- swift - 在 Swift 4 中本地保存图像并写入 api 的性能问题
- javascript - 如何使用javascript动态获取和设置SVG元素的ID
- django - 连接到 PostgreSQL 时,Django 中的“psycopg2”有什么用
- javascript - 接收来自 AJAX POST 请求的响应
- reactjs - 接口中有状态 React.Component 的打字稿定义
- javascript - 如何从 Code.Org AppLab 上的数据表中正确读取数组?
- django - related_name 第一次有效,但第二次无效