设计一个带有自定义搜索的项目,在列表中必须满足两个条件,例如(输入文本和选择标记选项)或(输入文本和日期选项)。
目前使用输入文本和日期选项进行搜索有效,但如果我使用输入文本进行搜索并选择标记选项,则会出现这些错误,
[u"'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
这是我的模特:
class OrderItem(models.Model):
order_type = models.CharField(max_length=50, blank=True, null=True)
certification_no = models.CharField(max_length=10, default="43534")
client = models.ForeignKey(Client, blank=True, null=True)
item = models.CharField(max_length=500)
quantity = models.PositiveIntegerField(default=1)
item_value = models.DecimalField(decimal_places=2, max_digits=7)
certification_type = models.CharField(max_length=200)
packing_list = models.FileField(upload_to='files/%Y-%m-%d', blank=True, null=True)
order = models.ForeignKey('Order', blank=True, null=True)
created_on = models.DateTimeField(auto_now_add = True)
这是我的form.py:
class OrderItemForm(ModelForm):
class Meta:
model = OrderItem
fields = ['client','item','quantity', 'item_value',
'certification_type', 'packing_list', 'certification_no']
以下是我的观点:
def search_form(request):
data = OrderItem.objects.order_by().values_list('certification_type', flat=True).distinct()
print data
if request.method == 'GET':
user_search = request.GET.get('searched_item')
print "What is searched for is : ", user_search
selected_tag = request.GET.get('certification_type')
print "The selected certificate type is :", selected_tag
start_date = request.GET.get('first_date')
print "The start date is : ", start_date
end_date = request.GET.get('second_date')
print "The end date is : ", end_date
if user_search != None and (start_date != None or end_date != None or selected_tag !=None):
items = OrderItem.objects.filter(
(Q(certification_type__iexact=selected_tag) | Q(created_on__range=(start_date , end_date)) ) ,
Q(order_type__iexact= user_search) | Q(item__iexact= user_search)
| Q(certification_no__iexact= user_search) | Q(client__user__email__iexact= user_search) | Q(client__phone_number__iexact= user_search)
| Q(id__iexact= user_search) | Q(created_on__iexact= user_search)
)
print "items ", items
return render(request, 'i/search_results.html', {'items':items, 'user_search_query':user_search, 'selected_tag_query': selected_tag, "data":data, "start_date": start_date, "end_date": end_date})
else:
return render(request, 'i/dashboard.html', {'error':True, "data":data})
else:
return render(request, 'i/dashboard.html', {'error':True, "data":data})
return render(request, 'i/search_results.html', {'items':items, 'user_search_query':user_search, 'selected_tag_query': selected_tag, "data":data})
不知道自己做错了什么,
答案 0 :(得分:0)
在视图中:
不是user_search != None
您应该使用user_search != ''
并在forms.py中使用此示例进行handel错误:
def clean_user_search(self):
test = self.cleaned_data.get('Name')
if test != '':
raise ValidationError('error')
return test