django db查询,得到错误

时间:2013-11-04 21:21:32

标签: python django django-models

这是我的模特:

 class company_profile(models.Model):
       user=models.ForeignKey(User, unique=True)
       company=models.CharField(max_length=200)

      def __unicode__(self):
            return self.company

当我得到django shell时,我做了以下工作,它工作正常:

  from my_app.models import company_profile
  everything = company_profile.objects.all()
  # this gives me the correct out put of the existing company names , 

但是当我执行以下操作时:

  username = company_profile.objects.all().filer(user='rakesh')

  this is the error , that i am getting : 

  ValueError: invalid literal for int() with base 10: 'rakesh'

我该如何解决这个问题,或者我的查询错误。

1 个答案:

答案 0 :(得分:5)

您的查询错误。 user="rakesh"子句需要获得User实例或User实例的PK。

您可能需要filter(user__username='rakesh')或其他内容,具体取决于User上的字段。