简体   繁体   English

Django-channels: AttributeError: 'str' object 没有属性 'profile'

[英]Django-channels: AttributeError: 'str' object has no attribute 'profile'

So I have my websockets in consumers, I did in consumers that when the websocket is receive it will update with +1 a FloatField in my models, the problem is that I dont know why is not working, here is my code:所以我在消费者中有我的 websockets,我在消费者中做过,当收到 websocket 时,它会在我的模型中更新为 +1 一个 FloatField,问题是我不知道为什么不起作用,这是我的代码:

models.py:模型.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:消费者.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()

the problem is that when a websocket is receive it doesn´t update the floatfield value and it give me this error:问题是,当收到 websocket 时,它不会更新浮点值,它给了我这个错误:

    notification = other_user.profile.objects.get(pk=1)
    AttributeError: 'str' object has no attribute 'profile'

does anybody know what the problem is?有人知道问题是什么吗?

You're passing a string into the variable other_user .您正在将一个字符串传递给变量other_user then trying to access your user model through a string object.然后尝试通过字符串 object 访问您的用户 model。 First of all, you need to use the non-static reference to the class model you setup in django models.py .首先,您需要使用在 django models.py中设置的 class model 的非静态引用。 Then, use the username you saved into the variable to search the database.然后,使用您保存到变量中的用户名来搜索数据库。

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

相关问题 Django AttributeError:“ str”对象没有属性“ model” - Django AttributeError: 'str' object has no attribute 'model' AttributeError:'str'对象在django中没有属性'get' - AttributeError: 'str' object has no attribute 'get' in django AttributeError: 'str' object 在 django 中没有属性 'strftime' - AttributeError: 'str' object has no attribute 'strftime' in django Django AttributeError: 'str' object 没有属性 'tzinfo' - Django AttributeError: 'str' object has no attribute 'tzinfo' 如何解决错误:'tuple' object has no attribute 'decode' with django-channels - How to solve the error: 'tuple' object has no attribute 'decode' with django-channels AttributeError'str'对象没有属性 - AttributeError 'str' object has no attribute AttributeError:'str'对象没有属性 - AttributeError: 'str' object has no attribute Django 2 中的迁移错误; AttributeError: 'str' 对象没有属性 'decode' - Migrations error in django 2; AttributeError: 'str' object has no attribute 'decode' AttributeError:'str'对象没有属性'regex'django抛出此错误 - AttributeError: 'str' object has no attribute 'regex' django throws this error Django 1.8:/:'str'对象的AttributeError没有属性'copy' - Django 1.8: AttributeError at /: 'str' object has no attribute 'copy'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM