Ejemplo n.º 1
0
def model_to_dict(model) -> dict:
    """1 dimensional json-ifyer for any Model"""
    from django.forms import model_to_dict as mtd
    # gets a field on the model, using the display name if available
    def get_display(model, field, default):
        method_name = 'get_{}_display'.format(field)
        return getattr(model, method_name)() if method_name in dir(model) else default

    return {k: get_display(model, k, v) for k, v in mtd(model).items()}
Ejemplo n.º 2
0
def model_to_dict(item):
    from django.forms import model_to_dict as mtd
    return dict((k, v) for (k, v) in mtd(item).items() if v is not None)
def model_to_dict(item):
    from django.forms import model_to_dict as mtd

    return dict((k, v) for (k, v) in mtd(item).items() if v is not None)