def model_fields(model, only=None, exclude=None, hidden=None, field_args=None, converter=None): """ Generate a dictionary of fields for a given Django model. See `model_form` docstring for description of parameters. """ from schematics.models import Model if not isinstance(model, Model): raise TypeError('model must be a schematics.Model schema') converter = converter or ModelConverter() field_args = field_args or {} gottago = wholelist() field_dict = { } if only: gottago = whitelist(*only) elif exclude: gottago = blacklist(*exclude) for field_name, field in model._fields.items(): if gottago(field_name, None): continue ishidden = False if hidden: if field_name in hidden: ishidden=True form_field = converter.convert(model, field, field_name, field_args.get(field_name), hidden=ishidden) if form_field is not None: field_dict[field_name] = form_field from pprint import pprint #pprint(field_dict) return field_dict
def export_loop(cls, instance_or_dict, field_converter, role=None, raise_error_on_role=False, print_none=False): """ Copy of schematics.transforms.export_loop (v0.9-5) The only difference: another `atoms` function is used, that completely excludes serializable fields, as they must not be stored in mongodb """ data = {} # Translate `role` into `gottago` function gottago = wholelist() if hasattr(cls, '_options') and role in cls._options.roles: gottago = cls._options.roles[role] elif role and raise_error_on_role: error_msg = u'%s Model has no role "%s"' raise ValueError(error_msg % (cls.__name__, role)) else: gottago = cls._options.roles.get("default", gottago) for field_name, field, value in atoms(cls, instance_or_dict): serialized_name = field.serialized_name or field_name # Skipping this field was requested if gottago(field_name, value): continue # Value found, apply transformation and store it elif value is not None: if hasattr(field, 'export_loop'): shaped = field.export_loop(value, field_converter, role=role, print_none=print_none) else: shaped = field_converter(field, value) # Print if we want none or found a value if shaped is None and allow_none(cls, field): data[serialized_name] = shaped elif shaped is not None: data[serialized_name] = shaped elif print_none: data[serialized_name] = shaped # Store None if reqeusted elif value is None and allow_none(cls, field): data[serialized_name] = value elif print_none: data[serialized_name] = value # Return data if the list contains anything if len(data) > 0: return data elif print_none: return data
def export_loop(cls, instance_or_dict, field_converter, role=None, raise_error_on_role=False, print_none=False): """ Copy of schematics.transforms.export_loop (v0.9-5) The only difference: another `atoms` function is used, that completely excludes serializable fields, as they must not be stored in mongodb """ data = {} # Translate `role` into `gottago` function gottago = wholelist() if hasattr(cls, '_options') and role in cls._options.roles: gottago = cls._options.roles[role] elif role and raise_error_on_role: error_msg = u'%s Model has no role "%s"' raise ValueError(error_msg % (cls.__name__, role)) else: gottago = cls._options.roles.get("default", gottago) for field_name, field, value in atoms(cls, instance_or_dict): serialized_name = field.serialized_name or field_name # Skipping this field was requested if gottago(field_name, value): continue # Value found, apply transformation and store it elif value is not None: if hasattr(field, 'export_loop'): shaped = field.export_loop(value, field_converter, role=role, print_none=print_none) else: shaped = field_converter(field, value) # Print if we want none or found a value if shaped is None and allow_none(cls, field): data[serialized_name] = shaped elif shaped is not None: data[serialized_name] = shaped elif print_none: data[serialized_name] = shaped # Store None if reqeusted elif value is None and allow_none(cls, field): data[serialized_name] = value elif print_none: data[serialized_name] = value # Return data if the list contains anything if len(data) > 0: return data elif print_none: return data
class Options: roles = { "public": wholelist(), }
class Options: roles = { 'draft': wholelist(), 'plain': wholelist() }
class Options: roles = {"draft": wholelist()}
class Options: serialize_when_none = True roles = {"create": wholelist(), "update": wholelist()}
class Options: serialize_when_none = True roles = {'remove_deprecated': wholelist()}