Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取作为序列化对象发送的请求用户的配置文件_Python_Django_Django Channels - Fatal编程技术网

Python 获取作为序列化对象发送的请求用户的配置文件

Python 获取作为序列化对象发送的请求用户的配置文件,python,django,django-channels,Python,Django,Django Channels,我将请求的用户对象发送到后台任务,该任务负责获取该用户的配置文件,然后计算配置文件的完整性。我可以发送序列化的用户对象,但无法获取该用户的配置文件。我该怎么做 消费者.py class AccountBackgroundTasks(SyncConsumer): def calculate_profile_percentage(self, context): print("arrived here successfully") logger.info("cont

我将请求的用户对象发送到后台任务,该任务负责获取该用户的配置文件,然后计算配置文件的完整性。我可以发送序列化的用户对象,但无法获取该用户的配置文件。我该怎么做

消费者.py

class AccountBackgroundTasks(SyncConsumer):
  def calculate_profile_percentage(self, context):
        print("arrived here successfully")
        logger.info("context", context)
        weight = {'full_name': 10, 'age': 10, 'city': 10, 'address': 10}
        total = 0
        try:
            user = context.get('user')
            profile_instance = model_to_dict(Profile.objects.get(user=user))
            for field in profile_instance:
                try:
                    total += weight[field]
                except AttributeError:
                    logger.error("Could not find the field")
                    continue
        except Profile.DoesNotExist:
            logger.error("Profile does not exist")
            return
        return total
query.py

@staticmethod
def resolve_profile(self, info, **kwargs):
    print('info', info.context.user)
    # type <class 'apps.accounts.models.User'>
    print('type', type(info.context.user))
    if info.context.user.is_authenticated:
        channel_layer = get_channel_layer()
        print("channel_layer", channel_layer)
        async_to_sync(channel_layer.send)('accounts', {
            'type': 'calculate.profile.percentage',
            'text': serializers.serialize('json', [info.context.user, ])
        })
        return Profile.objects.get(user=info.context.user)
    return None
@staticmethod
def解析_配置文件(自我、信息、**kwargs):
打印('info',info.context.user)
#类型
打印('type',type(info.context.user))
如果info.context.user.u经过身份验证:
通道层=获取通道层()
打印(“通道层”,通道层)
异步到同步(通道层发送)('accounts'{
'type':'calculate.profile.percentage',
“文本”:序列化程序。序列化('json',[info.context.user,])
})
返回Profile.objects.get(user=info.context.user)
一无所获

最好只发送用户的pk并从使用者的db中检索,因为这是一条跨进程传递的消息,尝试序列化模型对象不是一个好主意

最好只发送用户的pk并从使用者的db中检索,因为这是一条跨进程传递并尝试序列化的消息模型对象不是一个好主意,你可以在回答部分发布它,这样我就可以将它标记为已解决,好吗?我已经这样做了,如果我只传递id,那么要首先获取该用户的配置文件,我需要使用
user.models.object.get(id=id)
获取该用户,然后使用配置文件实例。这样可以吗?@Serenity您只需一次查询
Profile.objects.get(user_id=id)
就可以了,谢谢您的支持。我已将此标记为已解决。如果您有时间,请您看看这个问题好吗?