2

I was wondering can someone help me and clarify this error for me.

I am doing the Django REST Framework Tutorial, and I got to the part when I am creating a Serializer class.

from django.forms import widgets
from rest_framework import serializers
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES

class SnippetSerializer(serializers.ModelSerializer):
    pk = serializers.IntegerField(read_only=True)
    title = serializers.CharField(required=False,
                                  max_length=100)
    code = serializers.CharField(style={'type': 'textarea'})
    linenos = serializers.BooleanField(required=False)
    language = serializers.ChoiceField(choices=LANGUAGE_CHOICES,
                                       default='python')
    style = serializers.ChoiceField(choices=STYLE_CHOICES,
                                    default='friendly')

class Meta:
    model = Snippet
    fields = ('id', 'title', 'code', 'linenos', 'language', 'style')


def create(self, validated_attrs):
    """
    Create and return a new 'Snippet' instance, given the validated data.
    """
    return Snippet.objects.create(**validated_attrs)

def update(self, instance, validated_attrs):
    """
    Update and return an existing 'Snippet' instance, given the validated data.
    """
    instance.title = validated_attrs.get('title', instance.title)
    instance.code = validated_attrs.get('code', instance.code)
    instance.linenos = validated_attrs.get('linenos', instance.linenos)
    instance.language = validated_attrs.get('language', instance.language)
    instance.style = validated_attrs.get('style', instance.style)
    instance.save()
    return instance

Now mine problem is this, when I go in to the shell to serialize and when I want to import from snippets.serializers import SnippetSerializer the module throw this error:

>>> from snippets.serializers import SnippetSerializer

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/petarp/Documents/Django_Rest_Framework/serialization_tutorial/tutorial/snippets/serializers.py", line 6, in <module>
    class SnippetSerializer(serializers.ModelSerializer):
  File "/home/petarp/Documents/Django_Rest_Framework/serialization_tutorial/tutorial/snippets/serializers.py", line 10, in SnippetSerializer
    code = serializers.CharField(style={'type': 'textarea'})
  File "/home/petarp/.virtualenvs/env/local/lib/python2.7/site-packages/rest_framework/fields.py", line 468, in __init__
    super(CharField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'style'

Can someone please clarify this error and help me overcome this.

1
  • Post your Snippet Model too please Commented Dec 1, 2014 at 6:43

1 Answer 1

3

Use widget attribute do define form field type:

code = serializers.CharField(widget=forms.Textarea())

You're probably using DRF v2.x.x, which doesn't support style argument in Field class

Sign up to request clarification or add additional context in comments.

2 Comments

Your problem here is that you use the ModelSerializer (in the tutorial, they use the base serializers.Serializer). So the fields are not built in the same way and the 'style' keyword is not expected for the modelserializer : The serializer.CharField does only support 'widget' as mariodev said.
Tank you for the heads up, when I used you code @mariodev I head another Traceback saying mine forms are not defined, so I used 'code = serializers.CharField(widget=widgets.Textarea()', and I sould read more carefully next time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.