I have three models, each with a primary key. How can I perform join operation using rest framework in Django?
These are tables:
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
there are really good examples here:
http://www.django-rest-framework.org/api-guide/relations/
This page explains how you can include the authors names when serializing book, and also how to easily include a list of books when serializing the author.
If you want to be able to provide an API for publisher, you can easily provide a list of books using the methods in the link above.
http://www.django-rest-framework.org/api-guide/relations/#reverse-relations
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.