def fix_spare_parts(): spare_parts = session.query(SparePart).all() for spare_part in spare_parts: as_dict = spare_part.__dict__ as_dict['manufacturer_id'] = MongoManufacturer.find( manufacturer_id=spare_part.manufacturer_id).first_or_none()._id as_dict['supplier_id'] = MongoSupplier.find( supplier_id=spare_part.supplier_id).first_or_none()._id car_models = [] if spare_part.car_models is not None: for car_model in spare_part.car_models: car_models.append({ 'model_name': car_model.model_name, 'brand_name': car_model.brand_name }) as_dict.update({'car_models': car_models}) stores = [] for store in spare_part.stores: stores.append({ 'store_id': MongoStore.find(store_id=store.store_id).first_or_none()._id, 'stock': store.stock, 'stock_location': store.stock_location }) as_dict.update({'stores': stores}) as_dict['estimated_time_of_arrival'] = datetime.datetime( spare_part.estimated_time_of_arrival.year, spare_part.estimated_time_of_arrival.month, spare_part.estimated_time_of_arrival.day) del as_dict["_sa_instance_state"] mongo_spare_part = MongoSparePart(as_dict) mongo_spare_part.save()
def update_product(product: MongoSparePart, attribute_name, new_value): product[attribute_name] = new_value # product.__dict__[attribute_name] = new_value # product.__dict__.update({attribute_name: new_value}) # product.__setattr__(attribute_name, new_value) print(product.order_quantity) product.save()