当我尝试连接两个表时,django引发错误“类型对象'StudyRecord'没有属性'object'”

时间:2015-04-22 12:07:52

标签: django

当我尝试:

StudyRecord.object.filter(user__name__contains= id )

django投下以下错误:

  

/ error_study / my_infor /中的AttributeError   type对象'StudyRecord'没有属性'object'

回溯切换到复制并粘贴视图

    C:\Python27\lib\site-packages\django-1.7.3py2.7.egg\django\core\handlers\base.py in get_responseresponse = wrapped_callback(request, *callback_args, **callback_kwargs) ...
   ▶ Local vars
   c:\mystudio\tag_study\views.py in my_inforurrent_book = StudyRecord.object.filter(user__name__contains= id ) ...
   ▶ Local vars
StudyRecord模型:

    class StudyRecord(models.Model):
             user = models.ForeignKey(User)
             book = models.CharField(max_length=128)
             starttime= models.DateTimeField()
             endtime = models.DateTimeField()
        def save(self, *args, **kwargs):
             self.slug = slugify(self.number)
             super(StudyRecord, self).save(*args, **kwargs)

        def __unicode__(self):      #For Python 2, use __str__ on Python 3
             return self.book

这是观点

    def my_infor(request):
        if request.method == "GET":
            if request.user.is_authenticated():
               id = request.user.id
                StudyRecord.object.filter(user__name__contains= id )
                return HttpResponse(current_book)
        else:
            return HttpResponse("error2")
        context = RequestContext(request)
        return render_to_response('tag_study/my_infor.html', context)

2 个答案:

答案 0 :(得分:1)

它应该是对象,而不是对象

答案 1 :(得分:0)

从db获取数据时,您需要Manager来构建所需的QuerySet

  

每个模型至少有一个管理器,默认情况下称为对象。

更多信息here