def signup(self, request, *args, **kwargs): VALID_USER_FIELDS = [f.name for f in User._meta.fields] serialized = UserSerializer(data=request.data) if serialized.is_valid(): user_data = { field: data for (field, data) in request.data.items() if field in VALID_USER_FIELDS } user = User.objects.create_user(**user_data) return Response(UserSerializer(instance=user).data, status=status.HTTP_201_CREATED) else: return Response(status=status.HTTP_400_BAD_REQUEST)
class BookAPISerializer(serializers.ModelSerializer): category = CategoryBookSerializer(read_only=True) publisher = UserSerializer(read_only=True) class Meta: model = Book fields = '__all__'
class ArticleSerializer(serializers.ModelSerializer): author = UserSerializer(many=False, read_only=True) class Meta: model = Article fields = ('id', 'title', 'description', 'content', 'created_at', 'author', 'category')
class QuestionBankCreateUpdateSerializer(serializers.ModelSerializer): user = UserSerializer(read_only=True) questions = serializers.PrimaryKeyRelatedField(many=True, read_only=False, queryset=Question.objects) class Meta: model = QuestionBank fields = '__all__'
class UserSerialize(serializers.ModelSerializer): Provider = ServiceProviderSerializer(read_only=True) user = UserSerializer(read_only=True) class Meta: model = User fields = ('id', 'pk', 'email', 'name', 'provider', "user")
class ErrorLogSerializerList(serializers.ModelSerializer): agent = AgentSerializer() exception = AppExceptionSerializer() user = UserSerializer() class Meta: model = ErrorLog fields = '__all__'
class BoardSerializer(serializers.ModelSerializer): is_owner = serializers.SerializerMethodField() users = UserListSerializer(many=True, read_only=True) owner = UserSerializer(read_only=True) class Meta: model = Board fields = ('id', 'name', 'owner', 'is_owner', 'users') def get_is_owner(self, obj): return self.context['request'].user == obj.owner
class ProfileSerializer(serializers.ModelSerializer): user = UserSerializer() class Meta: model = Profile fields = ( 'id', 'user', 'profile_picture', 'bio', 'dob', 'casual_competitive', 'platform', 'game', )
class CommentSerializer(serializers.Serializer): user = UserSerializer(read_only=True) book = serializers.PrimaryKeyRelatedField(read_only=True) comment = serializers.CharField() time = serializers.DateTimeField(read_only=True) def create(self, validated_data): created = Comment(**validated_data) created.save() return created def update(self, instance, validated_data): instance.comment = validated_data.get('comment', instance.comment) instance.save() return instance