Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.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对象?_Python_Django_Recursion_Many To Many - Fatal编程技术网

Python 如何从递归多对多关系中检索Django对象?

Python 如何从递归多对多关系中检索Django对象?,python,django,recursion,many-to-many,Python,Django,Recursion,Many To Many,我有一个代表文章的Django类。有时一篇文章会评论一篇或多篇其他文章,我在类中使用递归多对多关系表示了这种关系,如下所示: class Article(models.Model): comments_on = models.ManyToManyField('self', blank=True) 我认为,通过在comments_on字段中选择多篇文章,我已经成功地在管理界面中将文章彼此关联。假设一篇文章(id1)对另外两篇文章(id2和id3)进行了评论。我不知道如何访问id1的co

我有一个代表文章的Django类。有时一篇文章会评论一篇或多篇其他文章,我在类中使用递归多对多关系表示了这种关系,如下所示:

class Article(models.Model): 
    comments_on = models.ManyToManyField('self', blank=True)
我认为,通过在comments_on字段中选择多篇文章,我已经成功地在管理界面中将文章彼此关联。假设一篇文章(id1)对另外两篇文章(id2和id3)进行了评论。我不知道如何访问id1的comments_on属性,以便id2和id3出现在我的Django模板中

我试过:

{{ [instance of Article object].comments_on }}
但我得到的只是:

[name of my app].Article.None

我想知道(1)如何访问此属性并使其显示?以及(2)如何一次访问一篇相关文章?例如,如果id1对id2和id3都有评论,我如何才能只显示id2或id3?

谢谢您的建议。根据您的意见,我解决了以下问题:

class Article(models.Model): 
    comments_on = models.ManyToManyField('self', blank=True)
在views.py中,我写道:

myvariable = Article.objects.all()
{% if item.comments_on.all.count == 1 %}
    {% for key in item.commented_on_by.all %}
      <strong>This study criticized:</strong> <a href="#">{{ key }}</a>
    {% endfor %}
 {% endif%}

  {% if item.comments_on.all.count > 1 %}
    <strong>This study criticized:</strong>
    <ul>
      {% for key in item.comments_on.all %}
        <li><a href="#">{{ key }}</a></li>
      {% endfor %}
    </ul>
  {% endif%}
然后将其传递到上下文字典,以便我可以在网页中呈现变量

在模板上,我写道:

myvariable = Article.objects.all()
{% if item.comments_on.all.count == 1 %}
    {% for key in item.commented_on_by.all %}
      <strong>This study criticized:</strong> <a href="#">{{ key }}</a>
    {% endfor %}
 {% endif%}

  {% if item.comments_on.all.count > 1 %}
    <strong>This study criticized:</strong>
    <ul>
      {% for key in item.comments_on.all %}
        <li><a href="#">{{ key }}</a></li>
      {% endfor %}
    </ul>
  {% endif%}
{%if.item.comments\u on.all.count==1%}
{item.all%}
这项研究批评:
{%endfor%}
{%endif%}
{%if item.comments_on.all.count>1%}
这项研究批评:
    {item.comments_on.all%}
  • {%endfor%}
{%endif%}
Well comments\u on是一个M2M字段,因此您必须对
文章中返回的queryset.comments\u on.all()
进行for循环,并显示它们的属性。谢谢。我试图在views.py中用myvariable=Article.comments_on.all()将其分配给函数中的一个变量,然后我尝试在html文件中循环使用myvariable,但我收到一个AttributeError,它说:“ManyToManyDescriptor”对象没有属性“all”。首先,
Article
是模型的类名
Article.comments\u on
将返回
ManyToManyDescriptor
函数。您只需调用
[instance of Article].comments\u on.all()
即可获得查询集。