@@ -254,23 +254,30 @@ def create_model_instance(model: Type[BaseModel], data: dict):
254
254
nested_fields = get_nested_fields (model )
255
255
256
256
# Handle flat models (like Pressure, LiquidLevel, pH) that have a single main field
257
- if len (model .__fields__ ) == 2 and "timestamp" in model .__fields__ :
258
- main_field = next (field for field in model .__fields__ if field != "timestamp" )
257
+ for field_name , _ in model .__fields__ .items ():
258
+ if field_name == "timestamp" :
259
+ continue
260
+ if field_name in nested_fields :
261
+ continue
262
+
259
263
lookup_field = (
260
264
model .collection_name () if hasattr (model , "collection_name" ) else model .__name__
261
265
)
262
- mongo_field = new_collections .get (lookup_field , main_field .lower ())
266
+ mongo_field = new_collections .get (lookup_field , field_name .lower ())
263
267
264
268
# Check if the mongo_field exists in the data
265
269
if mongo_field in data :
266
- data [main_field ] = data [mongo_field ]
267
- elif main_field .lower () in data :
268
- # If the main_field (lowercase) exists in data, use it
269
- data [main_field ] = data [main_field .lower ()]
270
+ data [field_name ] = data [mongo_field ]
271
+ elif field_name in data :
272
+ # If the field_name exists in data, use it
273
+ data [field_name ] = data [field_name ]
274
+ elif field_name .lower () in data :
275
+ # If the field_name (lowercase) exists in data, use it
276
+ data [field_name ] = data [field_name .lower ()]
270
277
else :
271
- # If neither the mongo_field nor the main_field exists, log an error
278
+ # If neither the mongo_field nor the field_name exists, log an error
272
279
logger .error (
273
- f"Field '{ mongo_field } ' or '{ main_field } ' not found in data for model { model .__name__ } "
280
+ f"Field '{ mongo_field } ' or '{ field_name } ' not found in data for model { model .__name__ } "
274
281
)
275
282
logger .error (f"Available fields in data: { list (data .keys ())} " )
276
283
# You might want to set a default value or raise an exception here
0 commit comments