"IndentationError: unindent does not match any outer indentation level" in python shell

Question:

I wrote a code in django-rest-framework, but got error. My class is –

class SnippetSerializer(serializers.Serializer):

    id = serializers.IntegerField(read_only=True)

    title = serializers.CharField(required=False, allow_blank=True, max_length=100)
    code = serializers.CharField(style={'base_template': 'textarea.html'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')

When I wrote “from snippets.serializers import SnippetSerializer” in python shell I got this error –
“IndentationError: unindent does not match any outer indentation level”

I searched for the solution, but got nothing. Please somebody help me.

Asked By: Sharif

||

Answers:

Disclaimer: ignore this answer as it does not respond to the question.

If you paste the following code in a Python shell:

class SnippetSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)

    title = serializers.CharField(required=False, allow_blank=True, max_length=100)
    code = serializers.CharField(style={'base_template': 'textarea.html'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')

You’ll run into issues because the Python shell will think your class definition ends right after the id line, at the first blank line.

To work around this, you need to remove the empty line:

class SnippetSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    title = serializers.CharField(required=False, allow_blank=True, max_length=100)
    code = serializers.CharField(style={'base_template': 'textarea.html'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')
Answered By: Linovia
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.