def map_related_to_field_and_label(field_name, related_model): """ Creates a mapping from the _id attribute of ForeignKeys to to the related attribute (e.g. built_form_id to built_form). It puts the related attribute in the main dict with its resource_uri and puts a label representation in the __labels dict so that the related attribute can map from its id to a label. This is similar to what the regular FeatureResource does but it is more manual since we only have the _id attribute and must resolve the related instance ourselves """ client_field_name = self.client_field_name(field_name) related_id = getattr( bundle.obj, client_field_name) if hasattr( bundle.obj, client_field_name) else None if related_id: related_model_instance = related_model.objects.get( id=related_id) related_field_name = field_name.replace('_id', '') return { # Get the resource class of the related model so we can make a uri related_field_name: FootprintResource.resolve_resource_class( related_model)().get_resource_uri( related_model_instance), # The label representation '__labels': { related_field_name: LayerSelection.resolve_instance_label( related_model_instance) } } return None
def process_dict(self, dct, bundle, outer_key=None): for key, value in (dct or {}).items(): updated_key = self.key_dehydrate_override().get(key, key) if updated_key != key: del dct[key] if isinstance(value, dict): dct[updated_key] = value if isinstance(value, dict): self.process_dict(value, bundle, outer_key=updated_key) else: # value is a model instance that is to be dehydrated to a resource_uri updated_model_instance = self.instance_dehydrate_override().get(updated_key, lambda x,y: value)(bundle.obj, value) # Find the resource field on the resource that matches one of these keys field = self.resolve_resource_field(*([outer_key, updated_key] if outer_key else [updated_key])) if field: field_resource = field.to_class() elif value: # Just search FootprintResource for a resource class that matches field_resource = FootprintResource.match_existing_resources(value.__class__)[0]() dct[updated_key] = field_resource.dehydrate_resource_uri(updated_model_instance) if value else None return dct
def map_related_to_field_and_label(field_name, related_model): """ Creates a mapping from the _id attribute of ForeignKeys to to the related attribute (e.g. built_form_id to built_form). It puts the related attribute in the main dict with its resource_uri and puts a label representation in the __labels dict so that the related attribute can map from its id to a label. This is similar to what the regular FeatureResource does but it is more manual since we only have the _id attribute and must resolve the related instance ourselves """ client_field_name = self.client_field_name(field_name) related_id = getattr(bundle.obj, client_field_name) if hasattr(bundle.obj, client_field_name) else None if related_id: related_model_instance = related_model.objects.get(id=related_id) related_field_name = field_name.replace('_id', '') return { # Get the resource class of the related model so we can make a uri related_field_name: FootprintResource.resolve_resource_class(related_model)().get_resource_uri(related_model_instance), # The label representation '__labels': {related_field_name: LayerSelection.resolve_instance_label(related_model_instance)} } return None