[PYTHON] Django filter summary

I was developing with Django the other day and needed a search function. However, I always asked, "Well, how to narrow down the writing?", So I summarized it. I would appreciate it if you could point out any additional notes or mistakes.

Constitution

Summary: Django filter

Django's model has an attribute called object, which contains an instance of the Manager class. filter is a function provided in this Manager class, which is useful when you want to ** filter (narrow down) the data you want literally.

Strict search

I have named it a strict search, but I will introduce a search that narrows down the search that exactly matches the content you want to filter.

This search finds ** case-sensitive exact matches **.

Example

user_name = "hoge" //Set the user name you want to find as a variable.
data = User.objects.filter(name=user_name)

Gets the name field in the User model that matches the user_name defined above. This will allow you to narrow down to just those that exactly match the name hoge.

Loosen search

Next, let's look at loose search. It's often the case when you say, "I don't know if hoge is the one that exactly matches the name, but if you're a person with a name like a messed up programmer called hogefugafoo."

In such a case, if you want to search not only for the exact match of "hoge" but also for ** including ** "hoge" **, this loose search is used.

How to write a loose search

is.

Let's see how to use it concretely.

Search including

user_name = "fuga"
data = User.objects.filter(name__contains=user_name)

Now you can also get "hogefugafoo". The same applies to other searches.

Search starting with

user_name = "fuga"
data = User.objects.filter(name__startswith=user_name)

In this case, since it starts with "fuga", "hogefugafoo" does not match, but "fuga Taro" does.

Search ending in

user_name = "fuga"
data = User.objects.filter(name__endswith=user_name)

In this case, "hogefugafoo" does not match, but "Yamada johnfuga" does.

Case insensitive

Now, at this point, you may want to do a looser search that doesn't distinguish between uppercase and lowercase letters. In such a case, just add ** ʻi` **.

Case-insensitive writing

-Search starting with ** **
Field name __istartswith = value

-Search ending in ** **
Field name __iendswith = value

Thank you for reading until the end. I will write about numerical search and AND search OR search with multiple conditions at a later date, so thank you for your cooperation.

Recommended Posts

Django filter summary
Django Summary
Django Summary
Python Django tutorial summary
About django custom filter arguments
Django
[Learning memo] Django command summary
Django Girls Tutorial Summary First Half
Django static file (static) related settings summary
Make a filter with a django template
django update
Django note 4
Python Summary
samba summary
Django memorandum
django search
Summary of frequently used commands of django (beginner)
Django test
python-pptx summary
Django # 2 (template)
Linux Summary
Django Note 5
Django tutorial summary for beginners by beginners ③ (View)
Django hands-on
Touch django
django notes
pyenv summary
String summary 1
Django basics
Django Shoho
Django defaults
Django + Docker
Django Glossary
pytest summary
matplotlib summary
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django tutorial summary for beginners by beginners ⑤ (test)
Django note 2
Django startup
Django notes
Django NullCharField
Django tutorial summary for beginners by beginners ⑥ (static file)
Django Tutorial Summary for Beginners by Beginners (Model, Admin)
[Django] Error when SlugField is specified in .filter ()
Django tutorial summary for beginners by beginners ④ (Generic View)
[Django3] Environment construction and various settings summary [Python3]