getting error in validating with serializers with foreign key relation django rest framework

class AddSubCategory_view(APIView):

def post(self, request):

        serializer = SubCategorySerializer(data =

        if serializer.is_valid():
            cat_Id ="cat_Id")
            SubCatNamee ="SubCatName")

            if not SubCategory.objects.filter( cat_Id_id   = 1 , SubCatName = SubCatNamee ).exists():
                SubCategory.objects.create( cat_Id_id   = 1 , SubCatName = SubCatNamee  )
                return Response( {
                    "status" : 200,
                    "message": "Sub Category Added" ,
                    "data" : ,
            else :
                return Response( {
                "status" : 400,
                "message": "Sub category already exists" ,
        else :
            return Response( {
            "status" : 400,
            "serializer Error" : serializer.errors
    except Exception as e:
        return Response( {
            "status" : 400,
            "catch Error" :  str(e) ,

Here is the output i am getting
Screenshot 2023-11-13 111927

here is the output i am getting in postman

class SubCategory(models.Model):
cat_Id = models.ForeignKey( Category , on_delete=models.CASCADE )
SubCatName = models.CharField(max_length=200)
date = models.DateField(auto_now_add=True)

class SubCategorySerializer(serializers.ModelSerializer):
cat_Id = serializers.IntegerField()
SubCatName = serializers.CharField(max_length=200)
if i comment this line there’s no error
cat_Id = CategorySerializer()

class Meta:
    model = SubCategory
    fields = ["id", "cat_Id", "SubCatName", "date"]  

if i comment this line its working but then i am getting error in get request
cat_Id = CategorySerializer()

This line of code is evaluating as False and causing that error to be returned. I suspect you’re running into issues because you have cat_Id_id=1 in that filter call. That is saying that you want to look at a specific foreign key which you probably shouldn’t. The foreign key ids may not be the same in every environment.

That said, you probably wanted: SubCategory.objects.filter(cat_Id_id=cat_Id, ...) to use the value that you’re submitting in the request.