- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 Django Rest Framework(通过 PUT 调用)更新自定义用户模型,虽然我可以正确更新模型的用户名和密码,但密码不会这样做:当我检查用户的通过 django admin 获取密码我收到“无效的密码格式或未知的哈希算法。”
这是我的views.py和serializers.py的相关部分:
class AccountViewSet(viewsets.ModelViewSet):
lookup_field = 'username'
queryset = Account.objects.all()
serializer_class = AccountSerializer
def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.IsAdminUser(),)
if self.request.method == 'POST':
return (permissions.AllowAny(),)
return (permissions.IsAdminUser(),)
def create(self, request):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid(raise_exception=True):
Account.objects.create_user(**serializer.validated_data)
return Response(serializer.validated_data, status = status.HTTP_201_CREATED)
return Response({
'status': 'Bad request',
'message': 'Account could not be created with received data.'
}, status = status.HTTP_400_BAD_REQUEST)
_
class AccountSerializer(serializers.HyperlinkedModelSerializer):
password = serializers.CharField(write_only = True, required = False)
confirm_password = serializers.CharField(write_only = True, required = False)
def validate(self, data):
if data.get('password') != data.get('confirm_password'):
raise serializers.ValidationError("Passwords do not match.")
else:
temp = Account(email = data.get('email'), username = data.get('username'))
validate_password(data.get('password'), temp)
return data
class Meta:
model = Account
fields = (
'id',
'email',
'username',
'created_at',
'updated_at',
'password',
'confirm_password'
)
read_only_fields = (
'created_at',
'updated_at'
)
def create(self, validated_data):
return Account.objects.create(**validated_data)
def update(self, instance, validated_data):
for attr, value in validated_data.items():
if attr == 'password':
instance.set_password(value)
else:
setattr(instance, attr, value)
instance.save()
update_session_auth_hash(self.context.get('request'), instance)
return instance
现在,我的理解是我不必重写 AccountViewSet 的 update() (由父类(super class) ModelViewSet 提供),因为它应该自动调用序列化器的 save(),而后者又应该调用序列化器的 update( )。由于它不起作用,我还尝试在 AccountViewSet 中实现我自己的 update() ,其风格与覆盖相应的 create() 方法相同,但这似乎也不起作用。事实上,在这两种情况下,我什至无法验证序列化器的 update() 是否被调用,这让我认为我的序列化器的 update() 方法没有被调用,而是调用了它的父类(super class)的 update() ,这会解释密码没有被散列。显然,就调用我自己的序列化器的 update() 方法而言,我做错了什么,但我不知道是什么,也找不到任何其他真正解决这个问题的答案。
最佳答案
我不知道您的代码的问题,但请尝试 https://thinkster.io/django-angularjs-tutorial 中的序列化器
class AccountSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=False)
confirm_password = serializers.CharField(write_only=True, required=False)
class Meta:
model = Account
fields = ('id', 'email', 'username', 'created_at', 'updated_at',
'first_name', 'last_name', 'tagline', 'password',
'confirm_password',)
read_only_fields = ('created_at', 'updated_at',)
def create(self, validated_data):
return Account.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.username = validated_data.get('username', instance.username)
instance.tagline = validated_data.get('tagline', instance.tagline)
instance.save()
password = validated_data.get('password', None)
confirm_password = validated_data.get('confirm_password', None)
if password and confirm_password and password == confirm_password:
instance.set_password(password)
instance.save()
update_session_auth_hash(self.context.get('request'), instance)
return instance
关于python - Django休息框架: Password not hashing properly on update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34717936/
假设我有两个集合资源: /persons /organizations 一个 GET至 /persons/id/返回一个特定的人。同样,一个 GET至 /organizations/id返回一个特定的
这个问题在这里已经有了答案: Rest best practice: when to return 404 not found (1 个回答) 关闭 7 年前。 我有以下 API: api/gues
背景: 我正在尝试创建一个系统,允许用户对其他用户撰写的评论进行投票(类似于 Reddit)。用户可以选择三个投票值:-1、0 或 1。我创建了一个 POST API(使用 django Rest-f
我正在尝试使用休息调用,该调用获取操作系统中文件的位置。作为返回,其余调用模拟文件的下载。 下面是代码 Welcome to CoinPay Click on bel
我正在向后端发送一个文件,这是上传代码: export class FileUploadComponent { @Input() multiple: boolean = false; @Vie
字符串用户年龄; while(true){ System.out.println("Enter user Age"); userAge = scInput.ne
我正在使用堆栈进行括号检查。包含 break; 语句的 if else 语句之一导致段错误。 我尝试删除 break 程序运行正常但打印错误答案,因为需要 break 才能打印正确的输出。这种段错误的
我是新手,正在学习编程,但无法正确“中断”它。如果硬币不是 0、5、10 或 25,它应该“打破”(最后一个 if)。该程序应该像自动售货机一样工作,只使用 10 美分、5 分硬币、25 美分硬币,当
每次“打破”for-each 结构(PHP/Javascript)时,我都觉得很脏 所以像这样: //Javascript 示例 for (object in objectList) { if
对于这段代码: type Callback = (err: Error | null, result: String) => void; let apiThatAcceptsCallback = (c
如何获取 teamcity build 的变化?我得到以下 URL,其中列出了所有构建更改并提供了一个 URL,我们可以使用该 URL 查看更改 http://teamcityserver/httpA
社区 我正在寻找有关富文本的一些建议。目前的问题是:在后端(在数据库中)存储和管理富文本内容的最佳方式是什么。为什么这看起来像个问题,因为我们可以有多个平台:桌面、移动、网络,这会带来问题。 据我所知
我在向 html 返回错误时遇到问题。所以,我有带有“sql解释器”的网络应用程序。 HTML 在解释器中输入查询后,我在 javascript 中运行 POST 并拍摄到 sprin
我正在尝试为 Rest Controller 进行单元测试。我为经理对数据库访问做了一个 stub (~mock),它运行良好。我唯一的问题是,当我开始单元测试时,它不会启动应用程序。 如何从我的单元
我想使用 azure blob Rest api 创建文件夹,并且还想检查文件夹是否存在。 Hierarchy: arjun/images/ arjun/Videos/001.avi arjun/Vi
我正在寻找使用 django Rest 和 Angular 处理用户登录的最佳方法。目前我正在 Controller 中执行此操作, $http.post('accounts/login/', $sc
我想设计一个允许客户端上传图像的 API,然后应用程序创建图像的不同变体,例如调整大小或更改图像格式,最后应用程序将每个变体的图像信息存储在数据库中。当我尝试确定实现此任务的正确策略时出现问题,这里有
我无法使用 Angular Ajax 连接我的服务器 web2py Restful,但如果我在浏览器中设置 url,它就可以工作,但我不能在 Angular ajax =( Angular 链接
我想在我的项目中添加一个 rest api,因此我需要一个选择性的 mysql 语句 bt 我总是回来 { “错误”:“(1054,你\“'where子句'中的未知列'无'\”)” 代码有什么问题?我
我想为自己创建一个启动/停止 Azure VM 机器人。我想做的是拥有一个 slack/telegram 机器人,它可以监听消息并通过命令/start/stop 启动/停止我的虚拟机。我应该使用什么
我是一名优秀的程序员,十分优秀!