In Django, filed lookups


In Django, field lookups are used to perform queries on model fields to retrieve specific data from the database. They allow you to filter, order, and search for records based on various conditions. Django provides a wide range of field lookups that you can use to customize your queries.


Here are some commonly used field lookups in Django:


1. Exact match lookup (`exact`):

  Author.objects.filter(name__exact='John Doe')


2. Case-insensitive exact match lookup (`iexact`):

  Author.objects.filter(name__iexact='john doe')


3. Contains lookup (`contains`):

  Book.objects.filter(title__contains='python')


4. Case-insensitive contains lookup (`icontains`):

  Book.objects.filter(title__icontains='python')


5. Greater than lookup (`gt`):

  Book.objects.filter(price__gt=10)


6. Less than lookup (`lt`):

  Book.objects.filter(price__lt=20)


7. Greater than or equal to lookup (`gte`):

  Book.objects.filter(price__gte=10)


8. Less than or equal to lookup (`lte`):

  Book.objects.filter(price__lte=50)


9. In lookup (`in`):

  Book.objects.filter(category__in=['fiction', 'mystery'])


10. StartsWith lookup (`startswith`):

  Author.objects.filter(name__startswith='J')


These are just a few examples of field lookups in Django. You can find a comprehensive list of available lookups in the Django documentation: [Field lookups](https://docs.djangoproject.com/en/3.2/ref/models/querysets/#field-lookups)


Remember to replace `Author` and `Book` with your actual model names and adjust the field names accordingly based on your model's structure.


Field lookups are powerful tools in Django that allow you to retrieve specific data from your database based on various conditions. You can combine multiple lookups and use them in conjunction with other query operations to refine your database queries.


date:Aug. 8, 2023