【发布时间】:2020-08-09 20:41:01
【问题描述】:
所以我在消费者中有我的 websocket,我在消费者中做了,当接收到 websocket 时,它会在我的模型中更新为 +1 一个 FloatField,问题是我不知道为什么不工作,这是我的代码:
models.py:
from django.contrib.auth.models import User
from django.db import models
from django.conf import settings
from django.utils.translation import gettext_lazy as _
class ProfileImage(models.Model):
"""
Profile model
"""
user = models.OneToOneField(
verbose_name=_('User'),
to=settings.AUTH_USER_MODEL,
related_name='profile',
on_delete=models.CASCADE
)
avatar = models.ImageField(upload_to='profile_image')
notifications = models.FloatField(default='0')
consumers.py:
async def websocket_receive(self, event):
# when a message is received from the websocket
print("receive", event)
# update the message notification badge val
other_user = self.scope['url_route']['kwargs']['username']
notification = other_user.profile.objects.get(pk=1)
notification.notifications = +1
notification.save()
问题是,当接收到 websocket 时,它不会更新 floatfield 值,它给了我这个错误:
notification = other_user.profile.objects.get(pk=1)
AttributeError: 'str' object has no attribute 'profile'
有人知道问题出在哪里吗?
【问题讨论】:
标签: python django websocket django-channels