How can I query many to many relationship in Django

Viewed 21

I have a many to many relationship Author, Post in Django.

What I want is, perform a query and get the results like displayed in the table.

How can I perform this action? Thanks for your support.

# models.py

class Author(models.Model):
    name = models.CharField(max_length=200)
    email = models.EmailField()

class Post(models.Model):
    authors = models.ManyToManyField(Author)
    headline = models.CharField(max_length=255)
    body_text = models.TextField(null=True, blank=True)

Tables

+------------+----------------+--------------------+
| author_id  | name           | email              |
+============+================+====================+
| 1          | John Doe       | jdoe@test.com      |
+------------+----------------+--------------------+
| 2          | Peter Smith    | psmith@test.com    |
+------------+----------------+--------------------+
| 3          | Karen Jackson  | kjackson@test.com  |
+------------+----------------+--------------------+


+----------+--------------------+--------------------------------------+
| post_id  | headline           | body                                 |
+==========+====================+======================================+
| 1        | Worpress tutorial  | Lorem Ipsum is simply dummy text...  |
+----------+--------------------+--------------------------------------+
| 2        | Developer news     | Lorem Ipsum is simply dummy text...  |
+----------+--------------------+--------------------------------------+
| 3        | Science news       | Lorem Ipsum is simply dummy text...  |
+----------+--------------------+--------------------------------------+

Result table

+----------------+------------+----------------+---------------------+----------+--------------------+-----------+
| autor_post_id  | author_id  | name           | email               | post_id  | headline           | body      |
+================+============+================+=====================+==========+====================+===========+
| 1              | 1          | John Doe       | jdoe@test.com       | 1        | Worpress tutorial  | Lorem...  |
+----------------+------------+----------------+---------------------+----------+--------------------+-----------+
| 2              | 2          | Peter Smith    | psmith@test.com     | 1        | Worpress tutorial  | Lorem...  |
+----------------+------------+----------------+---------------------+----------+--------------------+-----------+
| 3              | 1          | John Doe       | jdoe@test.com       | 2        | Developer news     | Lorem...  |
+----------------+------------+----------------+---------------------+----------+--------------------+-----------+
| 4              | 2          | Peter Smith    | psmith@test.com     | 2        | Developer news     | Lorem...  |
+----------------+------------+----------------+---------------------+----------+--------------------+-----------+
| 5              | 3          | Karen Jackson  | kjackson@test.com   | 3        | Science news       | Lorem...  |
+----------------+------------+----------------+---------------------+----------+--------------------+-----------+
| 6              | 1          | John Doe       | jdoe@test.com       | 3        | Science news       | Lorem...  |
+----------------+------------+----------------+---------------------+----------+--------------------+-----------+
1 Answers

this is easy:

Post.authors.through.objects.values_list('pk', 'author_id', 'author__name', 'author__email', 'post_id', 'post__headline', 'post__body')

other possibility is - to work with objects:

queyset=Post.authors.through.objects.select_related('author', 'post')

in queryset you find Through-object with bounded author and post.

Related