2

可能重复:
Django 获取模型的字段

我有下一个模型:

class People(models.Model):
    name = models.CharField(max_length=100)
    lastname = models.CharField(max_length=100)

我想遍历 People 表中的所有项目。

在views.py

- 当我尝试这个时:

  for each in People().objects.all():
     name=each.name

我收到了那个错误:

Manager isn't accessible via People instances

- 当我尝试这个时:

  for each in People():
     name=each.name

我收到此错误:

'People' object is not iterable

我该如何解决这个问题以及如何遍历我的 People 表中的所有项目?

4

1 回答 1

5

People.objects.all()

it works on the class not an instance People not People()

于 2012-09-24T02:12:28.583 回答