基数为10的int()的无效文字:'John Doe'

时间:2015-06-01 12:37:44

标签: django django-models

我有一个模型病人和另一个检查,现在我想根据名称检索病人的检查,但我用基础10:'John Doe'

对int()的错误无效文字进行粉刷
ValueError at /labtech/John Doe/see_exam
invalid literal for int() with base 10: 'John Doe'
Request Method: GET
Request URL:    http://homasoft.com:8000/labtech/John%20Doe/see_exam
Django Version: 1.8
Exception Type: ValueError
Exception Value:    
invalid literal for int() with base 10: 'John Doe'
Exception Location: /Library/Python/2.7/site-        packages/django/db/models/fields/__init__.py in get_prep_value, line 985
Python Executable:  /usr/bin/python
Python Version: 2.7.5
Python Path:    
['/Users/mymacbookpro/Documents/Virtualenvs/hospital/src',
'/Library/Python/2.7/site-packages/pip-1.4.1-py2.7.egg',
'/Users/mymacbookpro/Documents/Virtualenvs/hospital/src',
'/Users/mymacbookpro/Documents/Virtualenvs/hospital/src/django-social-auth',
'/Users/mymacbookpro/Documents/Virtualenvs/hospital/src/django-socialprofile',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat- darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
Server time:    Mon, 1 Jun 2015 12:27:27 +0000

这是我的观点

def see_exam(request, name):
    exam = Test.objects.filter(patient__user__exact=name)

    context = {
        'exam' : exam
    }

    return render(request, 'account/labtechs/see_results.html', context)

模特考试

class Test(models.Model):
    exam = models.ForeignKey(Exam)
    patient = models.ForeignKey(Patient)
    date = models.DateField()
    result = models.TextField(default="negative")
    done_by = models.CharField(max_length=120)
    def __unicode__(self):
        return self.exam.name

患者模型

class Patient(models.Model):
    """docstring for Patient"""
    user = models.OneToOneField(MyUser)
    date_of_birth = models.DateTimeField(blank=True, null=True)
    age = models.IntegerField(default=1)
    sex = models.OneToOneField(Sex)
    religion = models.CharField(max_length=120, blank=True, null=True)
    village = models.CharField(max_length=120)
    status = models.OneToOneField(Status, blank=True, null=True)
    relative = models.CharField(max_length=120)
    phone = models.IntegerField(default=1)
    allergies = models.TextField(default="", blank=True, null=True)
    defficiencies = models.TextField(default="", blank=True, null=True)
    created_at = models.DateTimeField(auto_now=True)
    updated_at = models.DateTimeField(auto_now_add=True)
    def __unicode__(self):
        return "%s %s" %(self.user.first_name, self.user.last_name)

1 个答案:

答案 0 :(得分:0)

更改查询

exam = Test.objects.filter(patient__user__exact=name)

exam = Test.objects.filter(patient__user__name__exact=name)
#------------------------------------------^

注意添加了user__name。在这里,我假设您的MyUser类具有您要比较的属性name

在您的查询中,您尝试将字符串name与不兼容的对象user(或该对象的内部ID)进行比较。