我们的项目要做个简单的用户推荐功能
简单来说就是根据用户经常使用哪项功能来为用户进行针对性的推荐
前言
系统划分十项功能,根据用户经常使用哪项功能来为用户进行针对性的推荐,用户对功能的使用率是通过点击次数或者说使用次数来体现的以下是本篇文章正文内容,下面案例可供参考
一、基本原理
- 在数据库的user用户表中添加int型的十个字段,分别代表该用户对各项功能的使用次数
- 在后端添加两类接口,一是十个功能次数接口,用户每使用一项功能就调用指定的功能次数接口,将数据库中对应项+1,二是推荐功能获取接口,根据用户的使用次数对十项功能进行排序,并将使用次数最多的三项功能推荐给用户
二、sql数据库
recom1~recom10十个字段分别代表用户对十项功能的使用次数
三、接口实现
1.子路由
urlpatterns = [
#十个功能次数接口
path('recom1',Recom1APIView.as_view()),
path('recom2',Recom2APIView.as_view()),
path('recom3',Recom3APIView.as_view()),
path('recom4',Recom4APIView.as_view()),
path('recom5',Recom5APIView.as_view()),
path('recom6',Recom6APIView.as_view()),
path('recom7',Recom7APIView.as_view()),
path('recom8',Recom8APIView.as_view()),
path('recom9',Recom9APIView.as_view()),
path('recom10',Recom10APIView.as_view()),
#功能推荐接口
path('recom_funcs',Recom_FuncsAPIView.as_view()),
]
2.视图函数
#获取三个推荐
class Recom_FuncsAPIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
userinfo = User.objects.get(useraccount=useraccount)
serializer = UserModelSerializer(instance=userinfo)
if serializer.data!=[]:
fuclist=[]
print(serializer.data)
print(serializer.data['recom1'])
r1 = {'lnum':1,'lv':serializer.data['recom1']}
r2 = {'lnum': 2, 'lv': serializer.data['recom2']}
r3 = {'lnum': 3, 'lv': serializer.data['recom3']}
r4 = {'lnum': 4, 'lv': serializer.data['recom4']}
r5 = {'lnum': 5, 'lv': serializer.data['recom5']}
r6 = {'lnum': 6, 'lv': serializer.data['recom6']}
r7 = {'lnum': 7, 'lv': serializer.data['recom7']}
r8 = {'lnum': 8, 'lv': serializer.data['recom8']}
r9 = {'lnum': 9, 'lv': serializer.data['recom9']}
r10 = {'lnum': 10, 'lv': serializer.data['recom10']}
t=[r1,r2,r3,r4,r5,r6,r7,r8,r9,r10]
t.sort(key=operator.itemgetter('lv'), reverse = True) # 默认为升序, reverse=True为降序
fuclist = [t[0]['lnum'],t[1]['lnum'],t[2]['lnum']]
return Response(fuclist,status=HTTP_200_OK)
else:
return Response({"message": "数据为空"}, status=HTTP_204_NO_CONTENT)
#浏览次数+1
class Recom1APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom1 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
class Recom2APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom2 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
class Recom3APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom3 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
class Recom4APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom4 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
class Recom5APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom5 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
class Recom6APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom6 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
class Recom7APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom7 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
class Recom8APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom8 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
class Recom9APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom9 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
class Recom10APIView(APIView):
def post(self,request):
useraccount = request.data['useraccount']
try:
userinfo =User.objects.get(useraccount=useraccount)
except:
return Response({"message":"用户不存在!"},status=HTTP_204_NO_CONTENT)
userinfo.recom10 += 1
userinfo.save()
return Response({"message": "信息修改成功!"}, status=HTTP_200_OK)
四、测试
使用Postman进行测试
成功获取到了三个用户“最常”使用的功能