Let's assume we have a model (Food). I actually love food pardon my manners.
class Food(models.Model):
food_id = models.IntegerField()
name = models.CharField(max_length=200)
I'm going to limit it to only the name and id id field for this example. We'll add more fields if necessary for other examples.
So, let's get down to business.
Define a function and Set your query string variable:
from .models import Food
def home(request):
food = request['food']
getFood = Food.objects.filter(name__exact=food)
return render(request, urlPath)
For this query, you are querying the Food table to retrieve results that match exactly what the user enters into the input field. That means, if what is in the database is "Afang Soup" if the user searches for "AFanG SOuP", "afang souP, "AFANG SOUP", "Afang soup" or anything other than "Afang Soup", it'll return an empty list
If nothing matches the string, an emapty list will be returned.
Notice that I'm using double underscore. if use anything other than that it won't work.
iexact is similar to exact. The only difference is that it capitalization doesn't matter here. "Afang Soup" will equal "afang soup", "AFANG SOUP", etc.
from .models import Food
def home(request):
food = request['food']
getFood = Food.objects.filter(name__iexact=food)
return render(request, urlPath)
As a rule of thumb, just know that each time you add "i" to the some of the query sets, you are saying it should ignore capitalization
- contains
"contains", like the name implies checks if a string is contained in the table being queried. So, imagine we have
- icontains
- in
- gt
- gte
- lt
- lte
- startswith
- istartswith
- endswith
- iendswith
- range
- year
- month
- day
- week_day
- isnull
- search
- regex
- iregex