Django REST Framework在关系中引用了自己的类

时间:2015-09-08 01:36:51

标签: python django django-rest-framework

我有一个与自身有多对多关系的文章模型:

class Article(models.Model):
  title = models.CharField(max_length=200)
  authors = models.ManyToManyField(User)
  abstract = models.TextField(max_length=2000, blank=True)
  full_text = models.TextField(blank=True)
  section = models.ManyToManyField(ArticleSection, null=True)
  references = models.ManyToManyField(ArticleReferences, null=True)
  related_articles = models.ManyToManyField("self", null=True)

  def __unicode__(self):
      return self.title

这是对于related_articles字段。我如何在rest-framework中进行同样的登录?

class ArticleSerializer(serializers.HyperlinkedModelSerializer):
  authors = AuthorSerializer(many=True,  read_only=True)
  section = ArticleSectionSerializer(many=True,  read_only=True)
  related_articles = ArticleSerializer(many=True,  read_only=True)
  class Meta:
      model = Article
      fields = (
              'title',
              'authors',
              'abstract',
              'full_text',
              'section',
              'related_articles'
              )

我收到没有定义ArticleSerializer的错误。我如何实现相关的文章字段?

0 个答案:

没有答案