[PYTHON] Django search

Search in Django

find.py


variable=Model name.objects.filter(Filter contents)

Filter contents

Character search

samle.py


variable=Model name.objects.filter(Filter contents)
#Perfect matching
variable=Model name.objects.filter(item name=value)
#Starts with a value
variable=Model name.objects.filter(item name__startswith=value)
#Search for things that end in a value
variable=Model name.objects.filter(item name__endswith=value)
#Fuzzy search
variable=Model name.objects.filter(item name__contains=value)
#Search regardless of size
variable=Model name.objects.filter(item name__iexact=value)
#Fuzzy search that does not distinguish between large and small
variable=Model name.objects.filter(item name__icontains=value)
variable=Model name.objects.filter(item name__istartswith=value)
variable=Model name.objects.filter(item name__iendswith=value)

Numerical comparison

sample.py


variable=Model name.objects.filter(Filter contents)
#Equal to value
variable=Model name.objects.filter(item name=int(value))
#Greater
variable=Model name.objects.filter(item name__gt=int(value))
#that's all
variable=Model name.objects.filter(item name__gte=int(value))
#Smaller
variable=Model name.objects.filter(item name__lt=int(value))
#Less than
variable=Model name.objects.filter(item name__lte=int(value))

AND search

sample.py


variable=Model name.objects.filter(First condition,Second condition,...)

variable=Model name.objects \
  .filter(First condition) \
  .filter(Second condition) \

OR search

sample.py


variable=Model name.objects.filter(Q(First condition)|Q(Second condition),...)

List search

sample.py


variable=Model name.objects.filter(item name__in=list)

Search in SQL

sample.py


variable=Model name.objects.raw(sql statement)

#sql statement
sql = 'SELECT *FROM table name'
##table name
Application name_Model name

Recommended Posts

django search
Django search
Django
django update
Django note 4
Django installation
Django Summary
Django test
Django # 2 (template)
Django Note 5
Touch django
django notes
Django Summary
Django basics
Django Shoho
Django defaults
Django + Docker
Django Glossary
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField
(Note) Template file search order in Django
Django doesn't search templates under the application
Django environment construction
Django Heroku Deploy 1
Django HTML Template # 2
Django Contact Form 2
Django begins part 1
Django model: ManyToManyField
[Linux] Search command
Models in Django
Django function-based view
Python Django Tutorial (5)
Django Learning Memo
Python Django Tutorial (2)
[Django] as_view () notes
First Django Challenge
django makemigarations createsuperuser
Django related sites
Internationalization with django
Django version check
django table creation
CentOS8 --Play --Django
django tutorial memo
visualize binary search
[Django] Redo migrate
django environment construction
Django Template notes
String search algorithm
Python Django Tutorial (8)
Python Django Tutorial (6)
django default settings
Start Django Tutorial 1
Django HTML template
Django Template Tips
Django girls-3 workflow