Skip to content

ezesundayeze/Django-Query-Set-Values

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

Django-Query-Set-Values

Django Query Set values and how to use them

  1. exact

How to use the exact keyword to query your database

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.

  1. iexact

How to use iexact

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

  1. contains

"contains", like the name implies checks if a string is contained in the table being queried. So, imagine we have

  1. icontains
  2. in
  3. gt
  4. gte
  5. lt
  6. lte
  7. startswith
  8. istartswith
  9. endswith
  10. iendswith
  11. range
  12. year
  13. month
  14. day
  15. week_day
  16. isnull
  17. search
  18. regex
  19. iregex

About

Django Query Set values and Use cases

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published