i have problem serializer. can create team , add existing user creates team 'group' team simultaneously. can't add users team later. i'm sending rest group name , user, receive user founded team. no changes in db.
i use django.auth user extend. relations between user , team manytomany.
should done in different way?
here code:
models.py
class user(abstractuser, guardianusermixin): pass class team(models.model): name = models.charfield(max_length=150) users = models.manytomanyfield('webapi.user', related_name='users') class meta: permissions = ( ("is_team_admin", "is team admin"), ("is_team_member", "is team member") )
serializers.py
class userserializer(serializers.modelserializer): password = serializers.charfield(write_only=true) def create(self, validated_data): user = user(**validated_data) user.set_password(validated_data['password']) user.save() return user def update(self, instance, validated_data): if 'password' in validated_data: password = validated_data.pop('password') instance.set_password(password) return super(userserializer, self).update(instance, validated_data) class meta: model = user fields = ('url', 'id', 'username', 'password', 'first_name', 'last_name') class teamserializer(serializers.modelserializer): users = userserializer(many=true, read_only=true) class meta: model = team fields = ('url', 'id', 'name', 'users')
views.py
class userviewset(viewsets.modelviewset): queryset = user.objects.all() serializer_class = userserializer def get_permissions(self): if self.request.method == 'post': self.permission_classes = (permissions.allowany, ) return super(userviewset, self).get_permissions() class teamviewset(viewsets.modelviewset): queryset = team.objects.all() serializer_class = teamserializer def perform_create(self, serializer): serializer.save(users=[self.request.user])
Comments
Post a Comment