Skip to content

Commit

Permalink
修复api创建/更新用户多对多字段bug (#1776)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick2wang authored Aug 22, 2022
1 parent acb9a56 commit 0341a69
Showing 1 changed file with 13 additions and 4 deletions.
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

0 comments on commit 0341a69

Please sign in to comment.