title: Django Rest Framework to receive specific product by field
body: <p>I am currently making an API to return JSON for a product.</p>
  <p>Currently using Django rest framework, I have successfully implemented the API to view all products via</p>
  <p>path/api/products</p>
  <p>to show a JSON of all products:</p>
  <pre><code>HTTP 200 OK
  Allow: GET, POST, HEAD, OPTIONS
  Content-Type: application/json
  Vary: Accept
  
  [
      {
          &quot;url&quot;: &quot;http://127.0.0.1:8000/api/products/1/&quot;,
          &quot;id&quot;: 1,
          &quot;brand&quot;: &quot;Mars&quot;,
          &quot;name&quot;: &quot;Barres&quot;,
          &quot;barcode&quot;: 5000159366168,
          &quot;category&quot;: &quot;Snacks, Snacks sucrés, Cacao et dérivés, Confiseries, Barres, Confiseries chocolatées, Barres chocolatées, Barres chocolatées au caramel&quot;,
          &quot;allergens&quot;: &quot;gluten,milk,soybeans&quot;,
          &quot;weight&quot;: 540.0,
          &quot;quantity&quot;: 1,
          &quot;footprint&quot;: 2.28655779803366e-06,
          &quot;perishable&quot;: false
      },
      {
          &quot;url&quot;: &quot;http://127.0.0.1:8000/api/products/2/&quot;,
          &quot;id&quot;: 2,
          &quot;brand&quot;: &quot;Twix&quot;,
          &quot;name&quot;: &quot;Twix&quot;,
          &quot;barcode&quot;: 5000159366267,
          &quot;category&quot;: &quot;Snacks, Snacks sucrés, Confiseries, Barres&quot;,
          &quot;allergens&quot;: &quot;gluten,nuts&quot;,
          &quot;weight&quot;: 600.0,
          &quot;quantity&quot;: 1,
          &quot;footprint&quot;: 0.0,
          &quot;perishable&quot;: false
      },
      {
          &quot;url&quot;: &quot;http://127.0.0.1:8000/api/products/3/&quot;,
          &quot;id&quot;: 3,
          &quot;brand&quot;: &quot;Twix&quot;,
          &quot;name&quot;: &quot;Twix salted caramel&quot;,
          &quot;barcode&quot;: 5000159528955,
          &quot;category&quot;: &quot;Biscuits et gâteaux, Biscuit chocolat&quot;,
          &quot;allergens&quot;: &quot;caramel, choclate, wheat&quot;,
          &quot;weight&quot;: 46.0,
          &quot;quantity&quot;: 1,
          &quot;footprint&quot;: 0.0,
          &quot;perishable&quot;: false
      }
  ]
  </code></pre>
  <p>However, I would like to be able to receive the JSON object of only one product by barcode:</p>
  <p>for example</p>
  <p>path/api/products/5000159366168
  or
  path/api/products/?barcode=5000159366168</p>
  <p>to return only the product that matches the barcode:</p>
  <pre><code>{
          &quot;url&quot;: &quot;http://127.0.0.1:8000/api/products/1/&quot;,
          &quot;id&quot;: 1,
          &quot;brand&quot;: &quot;Mars&quot;,
          &quot;name&quot;: &quot;Barres&quot;,
          &quot;barcode&quot;: 5000159366168,
          &quot;category&quot;: &quot;Snacks, Snacks sucrés, Cacao et dérivés, Confiseries, Barres, Confiseries chocolatées, Barres chocolatées, Barres chocolatées au caramel&quot;,
          &quot;allergens&quot;: &quot;gluten,milk,soybeans&quot;,
          &quot;weight&quot;: 540.0,
          &quot;quantity&quot;: 1,
          &quot;footprint&quot;: 2.28655779803366e-06,
          &quot;perishable&quot;: false
      }
  </code></pre>
  <p>Here is my code:</p>
  <p>my views.py:</p>
  <pre><code>class ProductViewSet(viewsets.ModelViewSet):
      queryset = Product.objects.all()
      serializer_class = ProductSerializer
  </code></pre>
  <p>my serializers.py:</p>
  <pre><code>from app1.models import Product
  from rest_framework import serializers
  class ProductSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = Product
          fields = [&quot;url&quot;,&quot;id&quot;, &quot;brand&quot;, &quot;name&quot;, &quot;barcode&quot;,&quot;category&quot;,&quot;allergens&quot;, &quot;weight&quot;, &quot;quantity&quot;, &quot;footprint&quot;,&quot;perishable&quot;]
  </code></pre>
  <p>my urls.py:</p>
  <pre><code>router = routers.DefaultRouter()
  router.register(r'products', views.ProductViewSet)
  
  
  urlpatterns = [
      ...
      path('api/', include(router.urls)),
      path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
  ]
  </code></pre>