filter with different operator in django

To filter with different operators in Django, you can use the filter() method along with different lookup operators. Here are the steps with explanations for each:

  1. Exact match (exact): Use __exact to match exact values in a field. python ModelName.objects.filter(field_name__exact=value)

  2. Case-insensitive exact match (iexact): Use __iexact to perform a case-insensitive exact match. python ModelName.objects.filter(field_name__iexact=value)

  3. Contains (contains): Use __contains to find records containing a specific value. python ModelName.objects.filter(field_name__contains=value)

  4. Case-insensitive contains (icontains): Use __icontains for a case-insensitive 'contains' search. python ModelName.objects.filter(field_name__icontains=value)

  5. Starts with (startswith): Use __startswith to filter records starting with a specific value. python ModelName.objects.filter(field_name__startswith=value)

  6. Ends with (endswith): Use __endswith to filter records ending with a specific value. python ModelName.objects.filter(field_name__endswith=value)

  7. Greater than (gt), Less than (lt), Greater than or equal to (gte), Less than or equal to (lte): Use __gt, __lt, __gte, and __lte for numerical or date-based comparisons. python ModelName.objects.filter(field_name__gt=value) ModelName.objects.filter(field_name__lt=value) ModelName.objects.filter(field_name__gte=value) ModelName.objects.filter(field_name__lte=value)

  8. In a list of values (in): Use __in to filter records where the field value is in a list of values. python ModelName.objects.filter(field_name__in=[value1, value2, ...])

  9. Not in a list of values (notin or exclude): Use ~Q(field_name__in=[value1, value2, ...]) or exclude() to filter records where the field value is not in a list of values. python ModelName.objects.filter(~Q(field_name__in=[value1, value2, ...])) ModelName.objects.exclude(field_name__in=[value1, value2, ...])

These examples cover various filtering options in Django using different lookup operators to customize your queries based on specific criteria.