The model defines an Article and an Author classes. They are linked together with a many-to-many relationship. This relationship is defined through an custom intermediary table:
# models.py
class Article(models.Model):
title = models.CharField(max_length=500)
authors = models.ManyToManyField(Author, through='AuthorOrder')
class Author(models.Model):
name = models.CharField(max_length=255)
class AuthorOrder(models.Model):
author = models.ForeignKey(Author)
article = models.ForeignKey(Article)
writing_order = models.IntegerField()
The serialization should return a JSON like this:
#articles_json
{"fields":
{
"title": "A title",
"authors": [
{
"name":"Author 1",
"writing_order": 1
},
{
"name":"Author 2",
"writing_order": 2
}
}
}
}
I've identified two solutions.
- This one suggests to serialize the AuthorOrder field separately.
- The second one is to use the Django Rest Framework.
I tried the twos but without success. Do you know another way to do it?