def get_arche_fields(res, extra_fields_name='extra_fields', arche_prop_name='arche_prop'): vals = [x for x in model_to_dict(res) if x.get('value') != ''] vals = [x for x in vals if x.get('value') != 'None'] vals = [x for x in vals if x.get('value') is not None] vals = [x for x in vals if x.get(extra_fields_name)] vals = [x for x in vals if x.get(extra_fields_name).get(arche_prop_name)] return vals
def as_graph(instance): """ serializes an object and its related (FK, M2M) objects as netvis-graph :param instance: An instance of a django model class :return: A dict with keys 'nodes' and 'edges' """ obj_dict = model_to_dict(instance) graph = {'nodes': [as_node(instance)], 'edges': []} for x in obj_dict: if x['f_type'] == 'FK' and x['value'] is not None: graph['nodes'].append(as_node(x['value'])) graph['edges'].append({ 'id': f"{as_node(instance)['id']}__{as_node(x['value'])['id']}", 'source': as_node(instance)['id'], 'target': as_node(x['value'])['id'], 'label': x['verbose_name'] }) elif x['f_type'] == 'M2M' and len(x['value']) > 0: for y in x['value']: graph['nodes'].append(as_node(y)) graph['edges'].append({ 'id': f"{as_node(instance)['id']}__{as_node(y)['id']}", 'source': as_node(instance)['id'], 'target': as_node(y)['id'], 'label': x['verbose_name'] }) elif x['f_type'] == 'REVRESE_RELATION' and len(x['value']) > 0: for y in x['value']: graph['nodes'].append(as_node(y)) graph['edges'].append({ 'id': f"{as_node(instance)['id']}__{as_node(y)['id']}", 'source': as_node(instance)['id'], 'target': as_node(y)['id'], 'label': x['verbose_name'] }) new_graph = add_node_types(graph) return new_graph
def field_dict(self): return model_to_dict(self)