Home > Software engineering >  Why am I getting "This field may not be null" errors when populating decimal and char fiel
Why am I getting "This field may not be null" errors when populating decimal and char fiel

Time:01-02

I am trying to populate DecimalField and CharField fields in a Django Model from serialized data via DRF Serializer, but I am getting strange errors of This field may not be null. Here is my model definition:

class Product(BaseModel):
    product = models.CharField(max_length=255)
    recommended_action = models.CharField(max_length=255)
    recommended_action_value = models.DecimalField(max_digits=12, decimal_places=8)
    recommended_price = models.DecimalField(max_digits=12, decimal_places=8)
    rrp = models.DecimalField(max_digits=12, decimal_places=8)
    iam_price = models.DecimalField(max_digits=12, decimal_places=8)
    iam_index = models.DecimalField(max_digits=12, decimal_places=8)
    factor = models.DecimalField(max_digits=12, decimal_places=8)
    avg_in_stock = models.DecimalField(
        null=True, blank=True, max_digits=12, decimal_places=8
    )

Here is my model serializer definition:

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Product
        fields = "__all__"

And here is my view:

@api_view(['POST'])
def migrate_data(request, *args, **kwargs):
    if request.method == "POST":
        data = json.loads(request.body)
        product_serialized_data = serializers.ProductSerializer(
            data=data,
            many=True,
            context={"request": request},
        )
        if not product_serialized_data.is_valid():
            print(product_serialized_data.errors)
    product_serialized_data.save()
    return Response(data={"detail": "Success"})

This is the data that I am passing to the POST request:

{
    "product": "DE_Ford_2095160",
    "recommended_action": "increase",
    "recommended_action_value": 0.0315553,
    "recommended_price": 14.5862,
    "rrp": 14.14,
    "iam_price": 6.56898,
    "iam_index": 0.464567,
    "factor": 2.15254,
    "avg_in_stock": 1
}

When I run this code, I get the following errors:

[
    {
        'recommended_action': [ErrorDetail(string='This field may not be null.', code='null')],
        'recommended_action_value': [ErrorDetail(string='This field may not be null.', code='null')],
        'recommended_price': [ErrorDetail(string='This field may not be null.', code='null')],
        'rrp': [ErrorDetail(string='This field may not be null.', code='null')],
        'iam_index': [ErrorDetail(string='This field may not be null.', code='null')],
        'factor': [ErrorDetail(string='This field may not be null.', code='null')]
    }
]

CodePudding user response:

because you're using serializers and APIs you must get the data from request.data not request.body, so your accessing the wrong data which is don't have the recommended_action field which case the error.

CodePudding user response:

I don't know the reason, but I just added null = True and blank = True in my Model, for those fields declaration which were showing error This may not be null. So the updated Model code which worked for me is below:

class Product(BaseModel):
    product = models.CharField(max_length=255)
    recommended_action = models.CharField(max_length=255, null=True, blank=Ture)
    recommended_action_value = models.DecimalField(max_digits=12, decimal_places=8, null=True, blank=Ture)
    recommended_price = models.DecimalField(max_digits=12, decimal_places=8, null=True, blank=Ture)
    rrp = models.DecimalField(max_digits=12, decimal_places=8, null=True, blank=Ture)
    iam_price = models.DecimalField(max_digits=12, decimal_places=8, null=True, blank=Ture)
    iam_index = models.DecimalField(max_digits=12, decimal_places=8, null=True, blank=Ture)
    factor = models.DecimalField(max_digits=12, decimal_places=8, null=True, blank=Ture)
    avg_in_stock = models.DecimalField(
        null=True, blank=True, max_digits=12, decimal_places=8
    )

I don't know the reason, but it worked for me. If anyone knows the reason, please explain so that me and other viewers of this question understand the cause of this issue.

  • Related