我可以使用一个搜索栏来搜索不同的东西吗?
Can I use a single search bar for searching different things?
我正在尝试使用单个搜索栏来搜索不同的模型。是否可以一视同仁?
我的views.py文件:
class SearchResultsView(ListView):
model = Food
template_name = 'restaurant/food_search_results.html'
context_object_name = 'data'
def get_queryset(self):
query = self.request.GET.get('search')
if query is not None:
return Food.objects.filter(name__icontains=query)
else:
return Food.objects.none()
我的 models.py 文件:
class Food(models.Model):
name = models.CharField(max_length=100)
image = models.ImageField(upload_to=upload_path)
description = models.TextField(max_length=500)
created = models.DateTimeField(auto_now_add=True)
meal_category = models.ManyToManyField(MealCategory, related_name="meal")
food_restaurant_category = models.ManyToManyField(FoodRestaurantCategory, related_name="food_cat")
class Restaurant(models.Model):
name = models.CharField(max_length=100)
您可以使用上下文传递第二个模型筛选列表:
class SearchResultsView(ListView):
model = Food
template_name = 'restaurant/food_search_results.html'
context_object_name = 'data'
def get_queryset(self):
query = self.request.GET.get('search')
if query is not None:
return Food.objects.filter(name__icontains=query)
else:
return Food.objects.none()
def get_context_data(self, **kwargs):
query = self.request.GET.get('search')
context = super(SearchResultsView, self).get_context_data(**kwargs)
if query is not None:
filtered_restaurants= Restaurant.objects.filter(name__icontains=query)
else:
filtered_restaurants= Restaurant.objects.none()
context.update({
'restaurants_list': filtered_restaurants
})
return context
我正在尝试使用单个搜索栏来搜索不同的模型。是否可以一视同仁?
我的views.py文件:
class SearchResultsView(ListView):
model = Food
template_name = 'restaurant/food_search_results.html'
context_object_name = 'data'
def get_queryset(self):
query = self.request.GET.get('search')
if query is not None:
return Food.objects.filter(name__icontains=query)
else:
return Food.objects.none()
我的 models.py 文件:
class Food(models.Model):
name = models.CharField(max_length=100)
image = models.ImageField(upload_to=upload_path)
description = models.TextField(max_length=500)
created = models.DateTimeField(auto_now_add=True)
meal_category = models.ManyToManyField(MealCategory, related_name="meal")
food_restaurant_category = models.ManyToManyField(FoodRestaurantCategory, related_name="food_cat")
class Restaurant(models.Model):
name = models.CharField(max_length=100)
您可以使用上下文传递第二个模型筛选列表:
class SearchResultsView(ListView):
model = Food
template_name = 'restaurant/food_search_results.html'
context_object_name = 'data'
def get_queryset(self):
query = self.request.GET.get('search')
if query is not None:
return Food.objects.filter(name__icontains=query)
else:
return Food.objects.none()
def get_context_data(self, **kwargs):
query = self.request.GET.get('search')
context = super(SearchResultsView, self).get_context_data(**kwargs)
if query is not None:
filtered_restaurants= Restaurant.objects.filter(name__icontains=query)
else:
filtered_restaurants= Restaurant.objects.none()
context.update({
'restaurants_list': filtered_restaurants
})
return context