def set_profile(request): '''修改用户信息,及用户配置''' user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) # 验证 user 表单的数据 if not user_form.is_valid(): raise stat.UserFormErr(user_form.errors) # 验证 profile 表单的数据 if not profile_form.is_valid(): raise stat.ProfileFormErr(profile_form.errors) # 修改用户数据 User.objects.filter(id=request.uid).update(**user_form.cleaned_data) # 修改 profile 数据 Profile.objects.update_or_create(id=request.uid, defaults=profile_form.cleaned_data) # 更新缓存 key = keys.MODEL_K % (Profile.__name__, request.uid) rds.delete(key) return render_json()
def set_profile(request): '''修改个人资料''' user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) # 检查数据有效性 if not user_form.is_valid(): raise stat.UserFormErr(user_form.errors) if not profile_form.is_valid(): raise stat.ProfileFormErr(profile_form.errors) print('-----------------') print(user_form.cleaned_data) print(request.uid) # 保存数据 User.objects.filter(id=request.uid).update(**user_form.cleaned_data) Profile.objects.filter(id=request.uid).update(**profile_form.cleaned_data) # 删除旧的缓存 key = keys.PROFILE_K % request.uid model_user_key = 'Model-User-%s'%(request.uid) model_profile_key = 'Model-Profile-%s'%(request.uid) rds.delete(key) rds.delete(model_user_key) rds.delete(model_profile_key) #方法2 # user = User.objects.get(id=request.uid) # user.__dict__.update(user_form.cleaned_data) # user.save() return render_json()
def set_profile(request): '''修改个人资料''' user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) # 检查数据有效性 if not user_form.is_valid(): raise stat.UserFormErr(user_form.errors) if not profile_form.is_valid(): raise stat.ProfileFormErr(profile_form.errors) # 保存数据 User.objects.filter(id=request.uid).update(**user_form.cleaned_data) Profile.objects.filter(id=request.uid).update(**profile_form.cleaned_data) # 删除旧的缓存 key = keys.PROFILE_K % request.uid rds.delete(key) # 文强 + 程超 的思路 # user = User.objects.get(id=request.uid) # user.__dict__.update(user_form.cleaned_data) # user.save() return render_json()
def set_profile(request): """修改个人资料""" user_from = UserForm(request.POST) profile_from = ProfileForm(request.POST) # 检查数据有效性 if not user_from.is_valid(): raise stat.UserFormErr(user_from.errors) if not profile_from.is_valid(): raise stat.ProfileFormErr(profile_from.errors) data = {} data.update(user_from.cleaned_data) data.update(profile_from.cleaned_data) data['birthday'] = str(data['birthday']) # 保存数据 User.objects.filter(id=request.uid).update(**user_from.cleaned_data) Profile.objects.filter(id=request.uid).update(**profile_from.cleaned_data) return render_json()
def set_profile(request): '''修改个人资料''' user_form = UserFrom(request.POST) profile_form = ProfileForm(request.POST) if not user_form.is_valid(): print(user_form.errors) raise stat.UserFormErr(user_form.errors) if not profile_form.is_valid(): raise stat.ProfileFormErr(profile_form.errors) # 保存数据 User.objects.filter(id=request.uid).update(**user_form.cleaned_data) Profile.objects.filter(id=request.uid).update(**profile_form.cleaned_data) # 数据修改过后删除旧的缓存 key = keys.PROFILE_KEY % request.uid rds.delete(key) return render_json()