2

I have an article model which has a many-to-many relationship with itself:

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

This is for the related_articles field. How would I do this same sort of login in the 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'
              )

I receive an error for ArticleSerializer not being defined. How would I implement the related articles field?

M.javid
  • 6,387
  • 3
  • 41
  • 56
byrdr
  • 5,197
  • 12
  • 48
  • 78
  • possible duplicate of [Django rest framework nested self-referential objects](http://stackoverflow.com/questions/13376894/django-rest-framework-nested-self-referential-objects) – Jared Mackey Sep 08 '15 at 02:09

0 Answers0