Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对于Django中的M2M关系,如何从'through'表中按字段排序?_Python_Django_M2m - Fatal编程技术网

Python 对于Django中的M2M关系,如何从'through'表中按字段排序?

Python 对于Django中的M2M关系,如何从'through'表中按字段排序?,python,django,m2m,Python,Django,M2m,我有两个模型——Note和Pinboard——在Django应用程序中具有多对多关系。这两个模型通过另一个模型(Pin)相互关联,因此我可以存储有关关系的附加信息 我想在插接板的DetailView中显示相关注释实例。这不是问题所在。但是我想预取笔记,并且在位置字段中通过表对笔记进行排序 有没有关于如何存档的提示(在第三张表上预取+排序) 实例 这就是我到目前为止所拥有的……它在某种意义上是有效的,我不必查询每个条目,但我发现没有任何方法可以按照Notes实例的位置对其进行排序,而不必对每个实例

我有两个模型——Note和Pinboard——在Django应用程序中具有多对多关系。这两个模型通过另一个模型(Pin)相互关联,因此我可以存储有关关系的附加信息

我想在插接板的
DetailView
中显示相关注释实例。这不是问题所在。但是我想预取笔记,并且
位置
字段中通过
表对笔记进行排序

有没有关于如何存档的提示(在第三张表上预取+排序)

实例 这就是我到目前为止所拥有的……它在某种意义上是有效的,我不必查询每个条目,但我发现没有任何方法可以按照Notes实例的
位置对其进行排序,而不必对每个实例进行更多的查询

型号

from django.db import models


class Note(models.Model):

    title = models.CharField(max_lenght=200)

    content = models.TextField()


class Pinboard(models.Model):

    title = models.CharField(max_lenght=200)

    notes = models.ManyToManyField(
        Note, blank=True, related_name='pinboards', through='Pin'
    )


class Pin(models.Model):

    class Meta:
        ordering = ['position', ]
        get_latest_by = 'position'

    pinboard = models.ForeignKey(Pinboard, related_name='note_pins')

    note = models.ForeignKey(Note, related_name='pinboard_pins')

    position = models.PositiveSmallIntegerField(default=0)
查看

from django.views.generic import DetailView


class PinboardDetailView(DetailView):

  model = Pinboard

  queryset = Pinboard.objects.prefetch_related('notes')
模板

{% extends 'base.html' %}
{% block content %}
<h1>{{ pinboard.title }}</h1>
{% if pinboard.notes.all.exists %}
    <ol>
    {% for note in pinboard.notes %}
        <li>{{ note.title }}</li>
    {% endfor %}
    </ol>
{% else %}
    <p>Nothing here yet…</p>
{% endif %}
{% endblock content %}
{%extends'base.html%}
{%block content%}
{{pinboard.title}
{%if pinboard.notes.all.exists%}
{pinboard.notes%中的注释为%s}
  • {{note.title}
  • {%endfor%} {%else%} 这里什么都没有

    {%endif%} {%endblock内容%}
    我建议您使用

    顺便说一下,您根本不需要在
    DetailView
    上使用
    prefetch\u related
    ,因为这将导致相同数量的查询

    另外,我建议您使用
    {%if pinboard.notes.all%}
    而不是
    {%if pinboard.notes.all.exists%}

    我建议您使用

    顺便说一下,您根本不需要在
    DetailView
    上使用
    prefetch\u related
    ,因为这将导致相同数量的查询

    另外,我建议您使用
    {%if pinboard.notes.all%}
    而不是
    {%if pinboard.notes.all.exists%}

    这里没有魔法:)与
    预回迁相关的
    方法有助于解决N+1查询问题,但在这种情况下N=1。请看这里没有魔术:)与预取相关的
    方法有助于解决N+1查询问题,但在本例中N=1。看见
    
    class PinboardDetailView(DetailView):
        model = Pinboard
        queryset = Pinboard.objects.prefetch_related(
            Prefetch(
                'notes',
                Note.objects.order_by('pinboard_pins__position'),
            )
        )