I've looked every answer in stack I cant find an answer. I 've created an API with django rest framework with some measurements . Im trying to post a TreeSensor measurement through postman or django API .Weather measurements works just fine but on my TreeSensor i get the following error even though they are identical besides the fields.
{
"soil_moisture_depth_1": [
"This field is required."
],
"soil_moisture_depth_2": [
"This field is required."
] }
Serializers :
class TreeSensorMeasurementSerializer(serializers.ModelSerializer):
class Meta:
model = TreeSensorMeasurement
fields = ["sensor", "datetime", "soil_moisture_depth_1","soil_moisture_depth_2","soil_moisture_depth_1_filtered","soil_moisture_depth_2_filtered", "soil_temperature"]
class WeatherStationMeasurementSerializer(serializers.ModelSerializer):
class Meta:
model = WeatherStationMeasurement
fields = ["sensor", "datetime", "wind_speed", "current_rain", "wind_direction", "solar_radiation", "air_temperature", "air_humidity", "air_pressure", "luminosity", "battery", "dew_point"]
API :
########### TreeSensorMeasurement Api ###########
def post(self,request, *args, **kwargs):
data = {
'sensor' : request.data.get('sensor'),
'datetime' : request.data.get('datetime'),
'soi_moisture_depth_1' : request.data.get('soil_moisture_depth_1'),
'soi_moisture_depth_2' : request.data.get('soil_moisture_depth_2'),
'soi_moisture_depth_1_filtered' : request.data.get('soil_moisture_depth_1_filtered'),
'soi_moisture_depth_2_filtered' : request.data.get('soil_moisture_depth_2_filtered'),
'soil_temperature' : request.data.get('soil_temperature'),
}
serializer = TreeSensorMeasurementSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status = status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
########### WeatherStationMeasurement Api ###########
def post(self,request, *args, **kwargs):
data = {
'sensor' : request.data.get('sensor'),
'datetime' : request.data.get('datetime'),
'wind_speed' : request.data.get('wind_speed'),
'current_rain' : request.data.get('current_rain'),
'wind_direction' : request.data.get('wind_direction'),
'solar_radiation' : request.data.get('solar_radiation'),
'air_temperature' : request.data.get('air_temperature'),
'air_humidity' : request.data.get('air_humidity'),
'air_pressure' : request.data.get('air_pressure'),
'luminosity' : request.data.get('luminosity'),
'battery' : request.data.get('battery'),
'dew_point' : request.data.get('dew_point'),
}
serializer = WeatherStationMeasurementSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status = status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
CodePudding user response:
I think the error occurred because you had changed the field names.
########### TreeSensorMeasurement Api ###########
def post(self,request, *args, **kwargs):
data = {
'sensor' : request.data.get('sensor'),
'datetime' : request.data.get('datetime'),
# Here I changed the field names
'soil_moisture_depth_1' : request.data.get('soil_moisture_depth_1'),
'soil_moisture_depth_2' : request.data.get('soil_moisture_depth_2'),
'soil_moisture_depth_1_filtered' : request.data.get('soil_moisture_depth_1_filtered'),
'soil_moisture_depth_2_filtered' : request.data.get('soil_moisture_depth_2_filtered'),
'soil_temperature' : request.data.get('soil_temperature'),
}
serializer = TreeSensorMeasurementSerializer(data=data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status = status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
The soi_...
field names should be changed into soil_...
.
Hope it could help.