Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework serializer representation not working

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>
like image 662
old_gare_bear Avatar asked Nov 01 '22 12:11

old_gare_bear


1 Answers

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
like image 149
related Avatar answered Nov 04 '22 06:11

related