title: Django REST Framework GET to retrieve rows that have NAME LIKE %NAME%
body: <p>So I have a table which I want to query using GET request and it should return me the items that match the %NAME% but currently the request returns me the complete table with all entries, not what I want.</p>
  <p><strong>request</strong></p>
  <pre><code>http://localhost:8000/inventory/?name=cookie/
  </code></pre>
  <p><strong>response</strong></p>
  <pre><code>GET /inventory/?name=cookie/
  HTTP 200 OK
  Allow: POST, OPTIONS, GET
  Content-Type: application/json
  Vary: Accept
  
  [
      {
          &quot;item_code&quot;: &quot;61f6ccca-e822-4b36-9ddf-5ec79a55a184&quot;,
          &quot;name&quot;: &quot;Golden Nutella Filled Cookie&quot;,
          &quot;price&quot;: 100,
          &quot;description&quot;: &quot;Delicious Nutella-filled homemade cookie which will satisfy your cravings.&quot;,
          &quot;image&quot;: &quot;https://images.unsplash.com/photo-1499636136210-6f4ee915583e?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=700&amp;q=80&quot;,
          &quot;quantity&quot;: 12
      },
      {
          &quot;item_code&quot;: &quot;ef6f68c4-c9a3-4a4e-8d5a-14d8f75a01ba&quot;,
          &quot;name&quot;: &quot;Fudgy Dark Brownie&quot;,
          &quot;price&quot;: 60,
          &quot;description&quot;: &quot;Fudgy and chewy dark chocolate passion.&quot;,
          &quot;image&quot;: &quot;https://bakerbynature.com/wp-content/uploads/2020/04/Cocoa-Fudge-Brownies-1-of-1.jpg&quot;,
          &quot;quantity&quot;: 16
      }
  ]
  </code></pre>
  <p><strong>expected response</strong></p>
  <pre><code>[
      {
          &quot;item_code&quot;: &quot;61f6ccca-e822-4b36-9ddf-5ec79a55a184&quot;,
          &quot;name&quot;: &quot;Golden Nutella Filled Cookie&quot;,
          &quot;price&quot;: 100,
          &quot;description&quot;: &quot;Delicious Nutella-filled homemade cookie which will satisfy your cravings.&quot;,
          &quot;image&quot;: &quot;https://images.unsplash.com/photo-1499636136210-6f4ee915583e?ixlib=rb-1.2.1&amp;ixid=eyJhcHBfaWQiOjEyMDd9&amp;auto=format&amp;fit=crop&amp;w=700&amp;q=80&quot;,
          &quot;quantity&quot;: 12
      },
  ]
  </code></pre>
  <p>MODEL: <strong>Inventory</strong></p>
  <pre><code>import uuid
  from django.db import models
  
  
  class Inventory(models.Model):
      item_code = models.UUIDField(
          primary_key=True, default=uuid.uuid4, editable=False)
      name = models.CharField(max_length=50, null=False)
      price = models.IntegerField(null=False)
      description = models.CharField(max_length=500)
      image = models.URLField()
      quantity = models.PositiveIntegerField(default=0, null=False)
  
      def __str__(self):
          return f'Item: {self.name} - Price:{self.price}'
  </code></pre>
  <p><strong>views.py</strong></p>
  <pre><code>@api_view(['GET', 'POST'])
  def inventory_list(request):
  
      if request.method == 'GET':
          inventory = Inventory.objects.all()
          serializer = InventorySerializer(inventory, many=True)
          return Response(serializer.data)
  
      elif request.method == 'POST':
          serializer = InventorySerializer(data=request.data)
  
          if serializer.is_valid():
              serializer.save()
              return Response(serializer.data, status=status.HTTP_201_CREATED)
          return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
  </code></pre>
  <blockquote>
  <p>EDIT (used name__icontains=name) for %NAME%</p>
  </blockquote>
  <pre><code>@api_view(['GET', 'PUT', 'DELETE'])
  def inventory_item(request, name):
      try:
          item = Inventory.objects.get(name__icontains=name)
      except Inventory.DoesNotExist:
          return Response(status=status.HTTP_404_NOT_FOUND)
  
      if request.method == 'GET':
          serializer = InventorySerializer(item)
          return Response(serializer.data)
  
      elif request.method == 'PUT':
          serializer = InventorySerializer(item, data=request.data)
          if serializer.is_valid():
              serializer.save()
              return Response(serializer.data)
          return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  
      elif request.method == 'DELETE':
          item.delete()
          return Response(status=status.HTTP_204_NO_CONTENT)
  
  </code></pre>
  <p><strong>serializers.py</strong></p>
  <pre><code>class InventorySerializer(serializers.ModelSerializer):
      class Meta:
          model = Inventory
          fields = ['item_code', 'name', 'price',
                    'description', 'image', 'quantity']
  </code></pre>
  <p>APP NAME: api <strong>urls.py</strong></p>
  <pre><code>urlpatterns = [
      path('', inventory_list),
      path('&lt;str:name&gt;/', inventory_item)
  ]
  
  </code></pre>
  <p><strong>urls.py</strong></p>
  <pre><code>urlpatterns = [
      path('admin/', admin.site.urls),
      path('inventory/', include('api.urls'))
  ]
  </code></pre>