判断用户是否是第一次登陆系统
middleware.py
from users.models import User class LoginINMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): response = self.get_response(request)
# 逻辑写在响应之后 try: user_id = response.data.get('data').get('user').get('id') except Exception: user_id = request.user.id if user_id is None: return response user = User.objects.get(id=user_id) if not user.logged_in: user.logged_in = True user.save() return response
settings.py
MIDDLEWARE = ( ......'LoginINMiddleware' )