好,所以我在Django中有这个模型:
class Training(models.Model):
url = models.URLField(max_length=100)
title = models.CharField(max_length=100)
topic = models.CharField(max_length=100)
grouping = models.CharField(max_length=100)
month = models.CharField(max_length=100)
overallOrder = models.CharField(max_length=100)
emailSent = models.CharField(max_length=100)
但是如何从这些字段中获取数据?
如果我放到sqlite外壳上,这就是我得到的:
In [1]: from myapp.models import Training
In [2]: queryset = Training.objects.all()
In [3]: print (queryset)
<QuerySet [<Training: Training object>]>
但是我希望能够查看该数据库中的所有数据。我不确定该怎么做。
感谢您的帮助。谢谢。
答案 0 :(得分:0)
添加 str 函数:
class Training(models.Model):
url = models.URLField(max_length=100)
title = models.CharField(max_length=100)
topic = models.CharField(max_length=100)
grouping = models.CharField(max_length=100)
month = models.CharField(max_length=100)
overallOrder = models.CharField(max_length=100)
emailSent = models.CharField(max_length=100)
def __str__(self):
return "Training with url %s and title %s"%(self.url, self.title)
您可以在函数中添加更多字段
答案 1 :(得分:0)
Django是“懒惰的”(请参见When is a Django QuerySet evaluated?)
这可以通过强制执行查询来绕过,例如通过请求nbr个元素,在您的示例中执行:
queryset.len()