Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复api创建/更新用户多对多字段bug #1776

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions sql_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@

class UserSerializer(serializers.ModelSerializer):
def create(self, validated_data):
user = Users(**validated_data)
user.set_password(validated_data["password"])
user.save()
return user
with transaction.atomic():
extra_data = dict()
for field in ("groups", "user_permissions", "resource_group"):
if field in validated_data.keys():
extra_data[field] = validated_data.pop(field)
user = Users(**validated_data)
user.set_password(validated_data["password"])
user.save()
for field in extra_data.keys():
getattr(user, field).set(extra_data[field])
return user

def validate_password(self, password):
try:
Expand All @@ -53,6 +60,8 @@ def update(self, instance, validated_data):
for attr, value in validated_data.items():
if attr == "password":
instance.set_password(value)
elif attr in ("groups", "user_permissions", "resource_group"):
getattr(instance, attr).set(value)
else:
setattr(instance, attr, value)
instance.save()
Expand Down