"This field is required." Django 休息框架
"This field is required." Django rest framework
我已经查看了堆栈中的每个答案我找不到答案。我已经用 django rest 框架创建了一个 API 并进行了一些测量。我正在尝试通过 postman 或 django API post TreeSensor 测量。天气测量工作正常但在我的 TreeSensor 上我收到以下错误,即使它们除了字段之外是相同的。
{
"soil_moisture_depth_1": [
"This field is required."
],
"soil_moisture_depth_2": [
"This field is required."
] }
序列化程序:
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)
我认为错误发生是因为您更改了字段名称。
########### 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)
soi_...
字段名称应更改为 soil_...
。
希望对您有所帮助。
我已经查看了堆栈中的每个答案我找不到答案。我已经用 django rest 框架创建了一个 API 并进行了一些测量。我正在尝试通过 postman 或 django API post TreeSensor 测量。天气测量工作正常但在我的 TreeSensor 上我收到以下错误,即使它们除了字段之外是相同的。
{
"soil_moisture_depth_1": [
"This field is required."
],
"soil_moisture_depth_2": [
"This field is required."
] }
序列化程序:
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)
我认为错误发生是因为您更改了字段名称。
########### 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)
soi_...
字段名称应更改为 soil_...
。
希望对您有所帮助。