django modelsTypeError:不支持解码str

时间:2017-07-22 08:09:46

标签: django django-models django-queryset

我是初学者,试图查看包含两条记录的基本查询集。我无法处理请求。寻求帮助。

class TestVenue(models.Model):
    venue_name      = models.CharField(max_length=40)
    venue_city      = models.CharField(max_length=20, null=True, blank=True)
    venue_province  = models.CharField(max_length=20, null=True, blank=True)
    venue_shortcode = models.CharField(max_length=20, null=True, blank=True)
    timestamp       = models.DateTimeField(auto_now_add=True)
    update          = models.DateTimeField(auto_now=True) 
    my_date_field   = models.DateField(auto_now=False, auto_now_add=False)

    def __str__(self):
        return self.venue_name

我的views.py非常简单

def venues_listview(request):
    template_name = 'venues_list.html'
    queryset = TestVenue.objects.all()
    context = {
        "object_list": queryset
        }
    return render(request, template_name, context)

从shell我想看到我的查询集但是我收到以下错误:

>>> from venues.models import TestVenue
>>> TestVenue.object.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: type object 'TestVenue' has no attribute 'object'
>>> TestVenue.objects.all()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\users\frank\desktop\test_env\lib\site-packages\django\db\models\query.py", line 229, in __repr__
    return '<%s %r>' % (self.__class__.__name__, data)
  File "C:\users\frank\desktop\test_env\lib\site-packages\django\db\models\base.py", line 589, in __repr__
    u = six.text_type(self)
  File "C:\users\frank\desktop\test_env\src\venues\models.py", line 14, in __str__
    #         return self.venue_name
TypeError: decoding str is not supported

2 个答案:

答案 0 :(得分:1)

您的错误就在这一行,

TestVenue.object.all()

应该是,

TestVenue.objects.all()

objects是调用默认管理器的属性,而不是object

答案 1 :(得分:0)

我重新启动了计算机,现在正在运行。我曾尝试启动和停止服务器,但这没有解决问题。谢谢你的帮助。