Ejemplo n.º 1
0
    def __init__(self, data, *args, **kwargs):
        """
        This returns a object that we send as json content using
        utils.serialize_to_json, that is a wrapper to json.dumps
        method using a custom class to handle models and querysets. Put your
        options to serialize_to_json in kwargs, other options are used by
        response.
        """
        if 'sort_keys' not in kwargs:
            kwargs['sort_keys'] = settings.DEBUG

        super(JSONResponse,
              self).__init__(content=serialize_to_json(data, *args, **kwargs),
                             content_type='application/json')
Ejemplo n.º 2
0
    def __init__(self, data, **kwargs):
        """
        This returns a object that we send as json content using
        utils.serialize_to_json, that is a wrapper to json.dumps
        method using a custom class to handle models and querysets. Put your
        options to serialize_to_json in kwargs, other options are used by
        response.
        """
        if 'sort_keys' not in kwargs:
            kwargs['sort_keys'] = settings.DEBUG

        super(JSONResponse, self).__init__(
            content=serialize_to_json(data, **kwargs),
            content_type='application/json'
        )
Ejemplo n.º 3
0
def get_models(request):
    models = get_app_models('main')
    model_id = request.POST.get('model_id', '')
    model = next(
        model for model in models if model._meta.model_name == model_id)

    rows = None
    rows = model.objects.all()
    table = []

    is_empty = False
    if not rows:
        default_row = model()
        default_row.save()
        is_empty = True
        rows = model.objects.all()

    for row in rows:
        new_row = {}
        for field_name, value in row.__dict__.iteritems():
            if field_name.startswith('_'):
                continue

            cell_type = ''
            field = model._meta.get_field_by_name(field_name)[0]
            verbose_name = field.verbose_name
            internal_type = field.get_internal_type()

            if internal_type == 'DateField':
                cell_type = 'date'
                value = str(value)
            elif internal_type == 'IntegerField':
                cell_type = 'int'
            elif internal_type == 'CharField':
                cell_type = 'text'
            elif internal_type == 'AutoField':
                cell_type = 'id'

            new_row.update({field_name: [value, cell_type, verbose_name]})
        table.append({'row': new_row, 'row_id': row.id})

    return {'models': serialize_to_json(table), 'model': model_id, 'is_empty': is_empty}