0

Models.py

Categories:

class Category_product(models.Model):
    category_name = models.CharField(max_length=200, unique=True)

    def __str__(self):
        return self.category_name  

Products:

class Warehouse(models.Model):
    category_product = models.ForeignKey(
    Category_product, on_delete=models.CASCADE)
    product_name = models.CharField(max_length=200, unique=True)
    condition = models.BooleanField(default=False)
    amount = models.IntegerField()
    barcode = models.BigIntegerField()
    f_price = models.CharField(max_length=255, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.product_name

urls.py

   path('products-list/', views.WarehouseList.as_view()),

Views.py

class WarehouseList(generics.ListCreateAPIView):
    queryset = Warehouse.objects.all()
    serializer_class = WarehouseSerializer

Serializers.py

# SERIALIZER OF CATEGORY PRODUCTS
class Category_productSerializer(serializers.ModelSerializer):
    class Meta:
        model = Category_product
        fields = ['id', 'category_name']


# SERIALIZER OF WAREHOUSE
class WarehouseSerializer(serializers.ModelSerializer):
category_name = serializers.ReadOnlyField(
    source='category_product.category_name')

def get_serializer(self, *args, **kwargs):
    if isinstance(kwargs.get('data', {}), list):
        kwargs['many'] = True
    return super(Category_productSerializer, self).get_serializer(*args, **kwargs)

class Meta:
    model = Warehouse
    fields = ['id', 'category_product', 'category_name', 'condition',
              'product_name', 'amount', 'barcode', 'f_price', 'created_at', 'updated_at']

I want to get products by exact category

For example: I have product category

{
"id": 1 
"category_name": "Electronics"
}

If I send GET request to api/products-list/?cat=1 I want to get products which have this category

1 Answer 1

1

Create a get_queryset method as follow.

class WarehouseList(generics.ListCreateAPIView):

    queryset = WareHouse.objects.all()
    serializer_class = WarehouseSerializer

    def get_queryset(self):
       cat = self.request.query_params.get('cat', None)       
       if cat is not None:
            self.queryset = self.queryset.filter(category_product__id=cat)
       return self.queryset

Sign up to request clarification or add additional context in comments.

9 Comments

Now it returns _'WarehouseList' should either include a queryset attribute, or override the get_queryset() method. _
Posted, after views.py
Thank's, now it works! I'll remember you when i became great developer:)
My suggestion for you is to learn python well, so that you will be able to read code of django rest framework. Then you can solve all drf related problems on your own. Granted, It will be difficult and take time. But there is no other way. Best of luck!
Where should I use many="True" if I want to POST multiple objects at once?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.