无法将关键字“carmodel”解析到字段中。选择包括:外部颜色、id、内部_

2024-09-27 07:18:45 发布

您现在位置:Python中文网/ 问答频道 /正文

由于我想对外键进行筛选,所以在views.py的方法dropdownlistsearch()中使用了carmodel和uuu。当运行代码时,它告诉它必须是一个字段。我不明白这部分。为什么会出现这个错误。我使用的是django1.6.5。你知道吗

型号.py

from django.db import models
class CarInfo(models.Model):0
    vin_number = models.CharField(max_length = 17)
    model = models.ForeignKey(CarModel)
    timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
    updated = models.DateTimeField(auto_now_add = False, auto_now = True)
    def __unicode__(self):
        return self.vin_number

    class CarModel(models.Model):
        model = models.CharField(max_length = 60)
        def __unicode__(self):
            return self.model

视图.py

def dropdownsearch(request):
    try:
        q = request.GET.get('q')
    except:
        q = None

    if q:
        cars = CarInfo.objects.filter(carmodel__model__contains=q)
        template ='productions/resultstwo.html'
    else:
        template = 'prodcutions/cars.html'
    context = {}

    return render(request,template,context)

汽车.html

<form action="/cars/s/" method="get">                        
    <select name="q">
    {% for info in modelinfos %}
        <option value="{{info.model}}">{{info.model}}</option>                                 
    {% endfor %}
    </select>  
</form>

Tags: pyselfinfoautomodelreturnmodelsrequest
2条回答

您正在对转发关系进行筛选,因此使用已定义的实际字段model。你知道吗

CarInfo.objects.filter(model__model__contains=q)

def testform(请求): 表格=CarInfo(请求.POST(或无) 如果表格.u有效吗(): 保存它=表单保存(commit=False) 保存_它。保存() 返回render\u to\u response(“产品”)/测试窗体.html“,locals(),context\u instance=RequestContext(请求))

相关问题 更多 >

    热门问题