【问题标题】:Django 1.6: ValueError invalid literal for int() with base 10Django 1.6:以 10 为基数的 int() 的 ValueError 无效文字
【发布时间】:2014-06-20 16:06:55
【问题描述】:

我正在尝试使用从最后一页获得的专业化输入按医生对象进行过滤,但我一直收到此错误

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "views.py" in doclistings
  87.     doctors = Doctor.objects.filter(specialization = s).order_by('-likes')


Exception Type: ValueError at /doclistings/
Exception Value: invalid literal for int() with base 10: 'Dentist'

这是我试图过滤它们的视图

def doclistings(request):
    d = getVariables(request)
    s = request.session.get('selection')
    d['userselection'] = s
    doctors = Doctor.objects.filter(specialization = s).order_by('-likes')
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')

 try:
        doctors = paginator.page(page)
    except PageNotAnInteger:
        doctors = paginator.page(1)
    except EmptyPage:
        doctors = paginator.page(paginator.num_pages)
    d['doctors'] = doctors
    d['paginator'] = paginator

    return render_to_response('meddy1/doclistings.html',d)

这里是医生模型

class Doctor(models.Model):
    name = models.CharField(max_length=100)
    specialization = models.ForeignKey(Specialization)
    clinic = models.ForeignKey(Clinic)
    seekers = models.ManyToManyField(User, through='UserContent')
    likes = models.IntegerField(default=0)

这里是专业化模型

class Specialization(models.Model):
    name = models.CharField(max_length=30)

这是我有表单的索引模板

<div class="signup">
          <div class="form-group">
            <form action="" method="post" >
            <select class="form-control" id="selection" name="selection">
              <option><b>Find a Doctor...</b></option>
              {% for value, text in form.selection.field.choices %}
                <option value="{{ value }}">{{ text }}</option>
              {% endfor %}
              {% csrf_token %}
            </select>
<span class="input-group-btn">
              <button class="btn btn-primary" type="submit"  name="submit" id="ss-submit">Find Doctors</button>
            </span>
          </div>
        </div>

我在 doclisting.html 中打印它以检查我得到的选择值

  <h2>{{userselection}}</h2> 

我只是想显示过滤具有专业化的医生对象并按最高喜欢排序。

【问题讨论】:

  • 失败的行是 doctors = Doctor.objects.filter(specialization = s).order_by('-likes') 所以,我的问题是,你确定你在变量 s 中有一个专门化对象吗?在这条指令s = request.session.get('selection') 之后,你有一个字符串或一个特化对象?
  • 我认为这是一个字符串。我在模板上打印,看看它是否真的获得了价值,它确实获得了价值。所以选择正在获得价值。我已经更新了上面的代码

标签: python django filter


【解决方案1】:

我认为你应该在你的views.py中这样做:

def doclistings(request):
    d = getVariables(request)
    s_name = request.session.get('selection')  # Change variable name
    d['userselection'] = s_name  # Update this for new variable name s_name
    spec = Specialization.objects.get(name=s_name)  # Get spec object
    # Now this should work:
    doctors = Doctor.objects.filter(specialization = spec).order_by('-likes')  
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')

 try:....

正如@Rohan 所说,你也可以这样做:

def doclistings(request):
    d = getVariables(request)
    s_name = request.session.get('selection')  # Change variable name
    d['userselection'] = s_name  # Update this for new variable name s_name
    # Now this should work:
    doctors = Doctor.objects.filter(specialization__name = s_name).order_by('-likes')  
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')

 try:....

在第二种方式中,您不需要获取专业化对象,您使用 specialization__name 告诉 Django 获取具有 specialization 的外键的医生的 name 等于 s_name

【讨论】:

  • 除了获取对象,还可以specialization__name=s_name
  • 太棒了!非常感谢您的帮助。
猜你喜欢
  • 2013-08-04
  • 2023-03-09
  • 2011-07-17
  • 1970-01-01
  • 2021-11-12
  • 2017-12-06
  • 2018-08-29
  • 1970-01-01
相关资源
最近更新 更多