折腾:
【已解决】Reactjs中如何使用浏览器的localStorage或sessionStorage保存json即dict字典对象的值
期间,需要去把之前的,冗余的信息,返回为单个的current的user,里面包含所有的需要的信息。
而不是放在前端去手动计算出来,尤其是把一个function group的list,却保存为多个字符串变量,再转换为需要的list,然后再把不同字段合并,很是麻烦。
期间:
【已解决】Django中数据serialize后不是字典对象而是OrderedDict的list
最后用:
@list_route(
methods=[‘get’],
url_path=’current_user’,
url_name=’current_user’,
serializer_class=EmptySerializer,
permission_classes=[
IsAuthenticated,
])
def current_user(self, request):
"""
当前用户,用户信息,用户组,是否为组长
"""
curUser = request.user
logger.info("current_user: curUser=%s", curUser)
userSerializer = UserSerializer(curUser, many=False)
logger.info("userSerializer=%s", userSerializer)
curUserDict = userSerializer.data
logger.info("curUserDict=%s", curUserDict)
allFunctionGroupList = FunctionGroup.objects.all()
# function_group_names = list(allFunctionGroupList.values_list(‘name’, flat=True))
logger.info("allFunctionGroupList=%s", allFunctionGroupList)
joinedFunctionGroupList = FunctionGroup.objects.filter(members=curUser)
logger.info("joinedFunctionGroupList=%s", joinedFunctionGroupList)
joinedFunctionGroupListSerializer = FunctionGroupSerializer(joinedFunctionGroupList, many=True)
logger.info("joinedFunctionGroupListSerializer=%s", joinedFunctionGroupListSerializer)
serializedJoinedFunctionGroupList = joinedFunctionGroupListSerializer.data
logger.info("serializedJoinedFunctionGroupList=%s", serializedJoinedFunctionGroupList)
curUserDict["joinedFunctionGroup"] = serializedJoinedFunctionGroupList
isScriptFunctionGroupOwner = False
ownedScriptFunctionGroup = None
for curJoinedFunctionGroup in serializedJoinedFunctionGroupList:
logger.info("type(curJoinedFunctionGroup)=%s", type(curJoinedFunctionGroup))
logger.info("type(curUser)=%s", type(curUser))
logger.info("curJoinedFunctionGroup=%s", curJoinedFunctionGroup)
groupFunctionValue = curJoinedFunctionGroup["function"]
logger.info("type(groupFunctionValue)=%s", type(groupFunctionValue))
logger.info("groupFunctionValue=%s", groupFunctionValue)
if groupFunctionValue == FunctionGroup.TYPE_NAME_SCRIPT:
groupOwner = curJoinedFunctionGroup["owner"]
logger.info("type(groupOwner)=%s", type(groupOwner))
logger.info("groupOwner=%s", groupOwner)
if groupOwner == curUser.username:
isScriptFunctionGroupOwner = True
ownedScriptFunctionGroup = curJoinedFunctionGroup
logger.info("isScriptFunctionGroupOwner=%s, ownedScriptFunctionGroup=%s",
isScriptFunctionGroupOwner, ownedScriptFunctionGroup)
break
# curUserDict["isScriptFunctionGroupOwner"] = isScriptFunctionGroupOwner
# curUserDict["ownedScriptFunctionGroup"] = ownedScriptFunctionGroup
curUserDict["scriptFunctionGroup"] = {
"isOwner": isScriptFunctionGroupOwner,
"ownedGroup": ownedScriptFunctionGroup
}
logger.info("updated curUserDict=%s", curUserDict)
# user_function_group_names = []
# user_function_group_ids = []
# user_function_group_owner = []
# user_function_group_functions = []
# for eachFunctionGroup in allFunctionGroupList:
# if eachFunctionGroup.members.all().filter(username=curUser.username).exists():
# user_function_group_names.append(eachFunctionGroup.name)
# else:
# user_function_group_names.append(”)
# if eachFunctionGroup.owner.username == curUser.username:
# user_function_group_owner.append(eachFunctionGroup.owner.username)
# else:
# user_function_group_owner.append(”)
# user_function_group_ids.append(eachFunctionGroup.id)
# user_function_group_functions.append(eachFunctionGroup.function)
# current_user_function_group = FunctionGroup.objects.filter(members=curUser)
# function_group_serializer = FunctionGroupSerializer(current_user_function_group, many=True)
# function_group_serializer_data = function_group_serializer.data
# logger.info("function_group_serializer_data=%s", function_group_serializer_data)
return Response(
{
‘current_user’: curUserDict,
# ‘current_user_function_group’: function_group_serializer_data,
# ‘current_user_function_group_list’: function_group_serializer_data,
# ‘function_group’: user_function_group_names,
# ‘function_group_ids’: user_function_group_ids,
# ‘function_group_owner’: user_function_group_owner,
# ‘function_group_functions’: user_function_group_functions,
然后返回后了前端需要的current的user的自己字段,加上额外的信息: