我正在關注Django Rest Framework tutorial on serializers,但發現了一些不尋常的行爲。當印刷再版(serializer_instance)顯然應該打印有用的檢查,我只能拿回這個表示:Django Rest Framework序列化表示不起作用
<snippets.serializers.SnippetSerializer object at 0x10220f110>.
My code似乎與本教程完全相同排隊,而我使用Django 1.7和Python 2.7。有誰知道爲什麼會發生這種情況?
片段/ 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')
片段/ 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',)
殼:
$ 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>
我還沒有看到我的殼冗長的「再版」輸出教程表明我們應該無論我使用的是什麼序列化器,我都會看到 - 使用Django 1.6。 – maxm