1
我正在開發一個django應用程序,可以根據食譜過濾配料。我使用Django過濾器來給我的用戶過濾選項。我的過濾下拉菜單工作得很好,但我想添加一個選項'全部',提交時應列出所有配料,而不管其配方如何。如何在過濾數據時在django過濾器中設置選項'All'
這裏是我的代碼:
#models.py
class Recipe(models.Model):
user = models.ForeignKey('auth.User')
title = models.CharField(max_length=500)
description = models.TextField(max_length=500)
rules = models.TextField(max_length=500,blank=True)
def __str__(self):
return self.title
class Ingredient(models.Model):
user = models.ForeignKey('auth.User')
recipe_id = models.ForeignKey(Recipe, on_delete=models.CASCADE)
title = models.CharField(max_length=500)
instructions = models.CharField(max_length=500)
rules = models.TextField(max_length=500,blank=True)
primal = models.CharField(default='0',max_length=500,blank=True)
def __str__(self):
return self.title
#forms.py
class RecipeFilter(django_filters.FilterSet):
class Meta:
model = Ingredient
fields = ['recipe_id']
#views.py
def ingredient_list(request):
ingredientfilter = IngredientFilter(queryset=Recipe.objects.filter(user = request.user))
if request.method == 'GET' and 'recipe_id' in request.GET:
recipe_id=request.GET['recipe_id'];
ingredients = Ingredient.objects.filter(recipe_id= recipe_id)
selected_combo_value = Recipe.objects.get(pk=recipe_id)
return render(request, 'ingredient_list.html',{'ingredients':ingredients, 'ingredientfilter': ingredientfilter,'selected_combo_value':selected_combo_value })
else:
ingredients = Ingredient.objects.filter(user = request.user)
return render(request, 'ingredient_list.html',{'ingredients':ingredients, 'ingredientfilter': ingredientfilter })
任何想法,該怎麼辦呢?