This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def to_internal_value(self, data): | |
| user_data = data['user'] | |
| return super().to_internal_value(user_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UserSerializer(serializers.ModelSerializer): | |
| def validate_password(self, value): | |
| if value.isalnum(): | |
| raise serializers.ValidationError('password must have atleast one special character.') | |
| return value | |
| def validate(self, data): | |
| if data['first_name'] == data['last_name']: | |
| raise serializers.ValidationError("first_name and last_name shouldn't be same.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UserSerializer(serializers.ModelSerializer): | |
| def validate_password(self, value): | |
| if value.isalnum(): | |
| raise serializers.ValidationError('password must have atleast one special character.') | |
| return value | |
| class Meta: | |
| model = User | |
| fields = ('username', 'email', 'first_name', 'last_name', 'password') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UserSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = User | |
| fields = ('username', 'email', 'first_name', 'last_name', 'password') | |
| extra_kwargs = { | |
| 'password': {'write_only': True} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UserSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = User | |
| fields = ('email', 'first_name', 'last_name') | |
| def to_representation(self, instance): | |
| representation = super().to_representation(instance) | |
| if instance.is_superuser: | |
| representation['admin'] = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UserSerializer(serializers.ModelSerializer): | |
| groups = serializers.SerializerMethodField() | |
| def get_groups(self, obj): | |
| groups = obj.groups.all() | |
| if not groups: | |
| return None | |
| return GroupSerializer(groups, many=True).data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UserSerializer(serializers.ModelSerializer): | |
| full_name = serializers.SerializerMethodField() | |
| def get_full_name(self, obj): | |
| return obj.get_full_name().upper() | |
| class Meta: | |
| model = User | |
| fields = ('email', 'first_name', 'last_name', 'full_name') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UserSerializer(serializers.ModelSerializer): | |
| first_name = serializers.SerializerMethodField() | |
| def get_first_name(self, obj): | |
| return obj.first_name.title() | |
| class Meta: | |
| model = User | |
| fields = ('email', 'first_name', 'last_name') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class GroupSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = Group | |
| fields = ('id', 'name') | |
| class UserSerializer(serializers.ModelSerializer): | |
| all_groups = GroupSerializer(source='groups', many=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.contrib.auth.models import Group | |
| class GroupSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = Group | |
| fields = ('id', 'name') |