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 许多字段数据无法渲染_Python_Django - Fatal编程技术网

Python 许多字段数据无法渲染

Python 许多字段数据无法渲染,python,django,Python,Django,我在访问Django模式数据时遇到了一个问题,所以这里t是我的模式 class People(models.Modal): name = models.CharField(max_length=30, choices=something, db_index=True) location = models.CharField(max_length=30, choices=something, db_index=True) class MyService(models.Model

我在访问Django模式数据时遇到了一个问题,所以这里t是我的模式

class People(models.Modal):
    name = models.CharField(max_length=30, choices=something, db_index=True)
    location = models.CharField(max_length=30, choices=something, db_index=True)


class MyService(models.Model):

    name = models.CharField(max_length=30, choices=something, db_index=True)
    peoples = models.ManyToManyField(People, null=True, blank=True)
这里是一些视图部分

services  = MyService.objects.all()
context['services'] = services
我正在尝试访问模板中的多对多字段数据,如

{% for service in services.peoples_set.all %}
        {{service.name}}
    {% endfor %}
我无法访问这些详细信息

请帮我找出我在这里做错了什么

谢谢

服务是QuerySet,不是模型实例

如果要显示服务的名称,请迭代服务:

如果要显示人名,则需要嵌套:


你这里有两个错误

首先,正如falsetru指出的,服务是一个查询集。也就是说,它是所有服务的容器,每个服务都有自己的人员集

其次,您已经在服务上直接定义了多对多字段,所以您使用了您在那里实际定义的字段名—人员—而不是反向关系

因此:


对象没有属性people\u set'instancemethod'对象不可编辑
{% for service in services %}
    {{service.name}}
{% endfor %}
{% for service in services %}
    {% for person in service.peoples.all %}
        {{ person.name }}
    {% endfor %}
{% endfor %}
{% for service in services %}
    {{ service.name }}
    {% for people in service.peoples.all %}
        {{ people.name }}
    {% endfor %}
{% endfor %}