我有很多关于关系的作者,“Django邮报”。
我想要的是,执行一个查询并获得显示在表中的结果。
我怎样才能执行这个动作?多谢你们的支持。
# 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)表
+------------+----------------+--------------------+
| 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... |
+----------+--------------------+--------------------------------------+结果表
+----------------+------------+----------------+---------------------+----------+--------------------+-----------+
| 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... |
+----------------+------------+----------------+---------------------+----------+--------------------+-----------+发布于 2022-09-07 22:51:32
这很简单:
Post.authors.through.objects.values_list('pk', 'author_id', 'author__name', 'author__email', 'post_id', 'post__headline', 'post__body')其他的可能性是-处理对象:
queyset=Post.authors.through.objects.select_related('author', 'post')在queryset中,您可以找到具有有界作者和post的通透对象。
https://stackoverflow.com/questions/73641772
复制相似问题