def _deserialize_store_model(self, fk_cache):
klass_model = _profile_models[self.profile][self.model_name]<EOL>if self.deleted:<EOL><INDENT>if self.hard_deleted:<EOL><INDENT>try:<EOL><INDENT>klass_model.objects.get(id=self.id).delete(hard_delete=True)<EOL><DEDENT>except klass_model.DoesNotExist:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>klass_model.objects.filter(id=self.id).delete()<EOL><DEDENT>return None<EOL><DEDENT>else:<EOL><INDENT>app_model = klass_model.deserialize(json.loads(self.serialized))<EOL>app_model._morango_source_id = self.source_id<EOL>app_model._morango_partition = self.partition<EOL>app_model._morango_dirty_bit = False<EOL>try:<EOL><INDENT>app_model.cached_clean_fields(fk_cache)<EOL>return app_model<EOL><DEDENT>except exceptions.ValidationError as e:<EOL><INDENT>logger.warn("<STR_LIT>".format(model=klass_model.__name__, id=app_model.id, error=e))<EOL>fk_ids = [getattr(app_model, field.attname) for field in app_model._meta.fields if isinstance(field, ForeignKey)]<EOL>for fk_id in fk_ids:<EOL><INDENT>try:<EOL><INDENT>st_model = Store.objects.get(id=fk_id)<EOL>if st_model.deleted:<EOL><INDENT>if st_model.hard_deleted:<EOL><INDENT>app_model._update_hard_deleted_models()<EOL><DEDENT>app_model._update_deleted_models()<EOL>return None<EOL><DEDENT><DEDENT>except Store.DoesNotExist:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>raise e<EOL><DEDENT><DEDENT>
When deserializing a store model, we look at the deleted flags to know if we should delete the app model.
Upon loading the app model in memory we validate the app models fields, if any errors occurs we follow
foreign key relationships to see if the related model has been deleted to propagate that deletion to the target app model.
We return:
None => if the model was deleted successfully
model => if the model validates successfully