[英]How to do join on 3 tables in Django Rest Framework (only primary keys)
I have three models, each with a primary key. 我有三个模型,每个模型都有一个主键。 How can I perform join operation using rest framework in Django? 如何在Django中使用rest框架执行联接操作?
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/ 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. 如果您希望能够为出版商提供API,则可以使用上面的链接中的方法轻松地提供书籍清单。
http://www.django-rest-framework.org/api-guide/relations/#reverse-relations http://www.django-rest-framework.org/api-guide/relations/#reverse-relations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.