【问题标题】:invalid literal for int() with base 10: 'John Doe'以 10 为底的 int() 的无效文字:'John Doe'
【发布时间】:2015-08-14 22:09:36
【问题描述】:

我有一个模型病人和另一个检查,现在我想根据名字检索病人的检查,但是我被错误的 int() 以 10: 'John Doe' 为底的无效文字所覆盖

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)

【问题讨论】:

标签: django django-models


【解决方案1】:

更改查询

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

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

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

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

【讨论】:

  • 不,有 attr 叫 name,有名字和姓氏,还有一个函数 get_full_name()
  • @user134186,那么您需要将提供的名称拆分为名字和姓氏,并按照建议进行比较。或者对它们进行startswithendswith 过滤
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
  • 2020-01-04
  • 2010-12-22
  • 2011-07-07
  • 2019-10-14
相关资源
最近更新 更多