由于我想对外键进行筛选,所以在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>
您正在对转发关系进行筛选,因此使用已定义的实际字段
model
。你知道吗def testform(请求): 表格=CarInfo(请求.POST(或无) 如果表格.u有效吗(): 保存它=表单保存(commit=False) 保存_它。保存() 返回render\u to\u response(“产品”)/测试窗体.html“,locals(),context\u instance=RequestContext(请求))
相关问题 更多 >
编程相关推荐