def alter_detail_data_to_serialize(self, request, bundle): # Show additional field following the model's rules rules = register_model_rules().model(self.get_model()).all() # All additional relationships for key in rules: # Filter rules to keep only Neomatch if isinstance(rules[key], Neomatch): bundle.data[key] = rules[key].query(bundle.obj.id) return bundle
def get_model_fields(model): fields = [] modelsRules = register_model_rules().model(model) if hasattr(model, "_meta"): # Create field object for fieldRules in modelsRules.fields(): try: f = model._meta.get_field(fieldRules.name) except FieldDoesNotExist: # This is rule field. Ignore it! continue # Ignores field terminating by + or begining by _ if not f.name.endswith("+") and not f.name.endswith("_set") and not f.name.startswith("_"): # Find related model for relation if hasattr(f, "target_model"): # We received a model as a string if type(f.target_model) is str: # Extract parts of the module path module_path = f.target_model.split(".") # Import models as a module module = __import__( ".".join(module_path[0:-1]), fromlist=["class"]) # Import the target_model from the models module target_model = getattr(module, module_path[-1], {__name__: None}) else: target_model = f.target_model related_model = target_model.__name__ else: related_model = None field = { 'name' : f.name, 'type' : f.get_internal_type(), 'help_text' : getattr(f, "help_text", ""), 'verbose_name' : getattr(f, "verbose_name", pretty_name(f.name)), 'related_model': related_model, 'rules' : fieldRules.all() } fields.append(field) return fields
def dehydrate(self, bundle): # Show additional field following the model's rules rules = register_model_rules().model( self.get_model() ) # Get the output transformation for this model transform = rules.get("transform") # This is just a string # For complex formating use http://docs.python.org/2/library/string.html#formatspec if type(transform) is str: transform = transform.format(**bundle.data) # We can also receive a function elif callable(transform): transform = transform(bundle.data) bundle.data["_transform"] = transform or getattr(bundle.data, 'name', None) # Control that every relationship fields are list # and that we didn't send hidden field for field in bundle.data: # Find the model's field modelField = getattr(bundle.obj, field, False) # The current field is a relationship if modelField and hasattr(modelField, "_rel"): # Wrong type given, relationship field must ouput a list if type(bundle.data[field]) is not list: # We remove the field from the ouput bundle.data[field] = [] # The field is a list of literal values elif type(modelField) in (list, tuple): # For tuple serialization bundle.data[field] = modelField # Get the output transformation for this field transform = rules.field(field).get("transform") # This is just a string # For complex formating use http://docs.python.org/2/library/string.html#formatspec if type(transform) is str: bundle.data[field] = transform.format(**bundle.data) # We can also receive a function elif callable(transform): bundle.data[field] = transform(bundle.data, field) return bundle