I'm following the Django Rest Framework tutorial on serializers, but finding some unusual behavior. Where print repr(serializer_instance) is apparently supposed to print useful inspection, I can only get back this representation:
<snippets.serializers.SnippetSerializer object at 0x10220f110>.
My code seems to line up exactly with that in the tutorial, and I'm using Django 1.7 and Python 2.7. Does anyone have any idea why this might be happening?
snippets/serializers.py:
from django.forms import widgets
from rest_framework import serializers
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
class SnippetSerializer(serializers.ModelSerializer):
class Meta:
model = Snippet
fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
snippets/models.py:
from django.db import models
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles
LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())
class Snippet(models.Model):
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, blank=True, default='')
code = models.TextField()
linenos = models.BooleanField(default=False)
language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
class Meta:
ordering = ('created',)
shell:
$ python manage.py shell_plus
>>> from snippets.serializers import SnippetSerializer
>>> serializer = SnippetSerializer()
>>> serializer
<snippets.serializers.SnippetSerializer object at 0x10220f110>
>>> print repr(serializer)
<snippets.serializers.SnippetSerializer object at 0x10220f110>
I had the same problem until I realized that I had version 2.4 installed.
Simply read the release notes and update to version 3.0
E.g. if you use a requirements.txt, change the rest framework line to :
djangorestframework==3.0
and run
pip install -r requirements.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With