Beispiel #1
0
def save_form(request, **kwargs):
    meta = kwargs.get('meta')
    model = apps.get_model(*meta.get('model').split('.'))
    fkwargs = {}

    if meta.get('formClass'):
        form_class = import_class(meta.get('formClass'))
    else:
        form_class = modelform_factory(model)

    if meta.get('formPrefix'):
        fkwargs['prefix'] = meta.get('formPrefix')

    form = form_class(kwargs.get('data'), **fkwargs)

    if form.is_valid():
        form.save()
        out = {'error': False}
    else:
        messages = []
        for field_name_error, errors_field in form.errors.items():
            for error in errors_field:
                messages.append({
                    'field_name': field_name_error,
                    'message': six.text_type(error)
                })

        out = {'error': True, 'messages': messages}

    return simplejson.dumps(out)
Beispiel #2
0
def save_form(request, **kwargs):
    meta = kwargs.get('meta')
    model = get_model(*meta.get('model').split('.'))
    fkwargs = {}

    if meta.get('formClass'):
        form_class = import_class(meta.get('formClass'))
    else:
        form_class = modelform_factory(model)

    if meta.get('formPrefix'):
        fkwargs['prefix'] = meta.get('formPrefix')

    form = form_class(kwargs.get('data'), **fkwargs)

    if form.is_valid():
        form.save()
        out = {'error': False}
    else:
        messages = []
        for field_name_error, errors_field in form.errors.items():
            for error in errors_field:
                messages.append({
                    'field_name': field_name_error,
                    'message': unicode(error)
                })

        out = {'error': True, 'messages': messages}

    return simplejson.dumps(out)
    def get_form(self):
        fkwargs = {}
        if self.kwargs.get("form"):
            # Custom form
            fkwargs["form"] = import_class(self.kwargs.get("form"))
        else:
            # Generic form
            fkwargs["form"] = get_dynamic_modelform(exclude=(self.query_field,))

        return modelform_factory(self.model, **fkwargs)
Beispiel #4
0
    def get_form(self):
        fkwargs = {}
        if self.kwargs.get('form'):
            # Custom form
            fkwargs['form'] = import_class(self.kwargs.get('form'))
        else:
            # Generic form
            fkwargs['form'] = get_dynamic_modelform(\
                                exclude=(self.query_field, ))

        return modelform_factory(self.model, **fkwargs)
Beispiel #5
0
    def get_form(self):
        """Creates and returns a form on the fly from the model using
        `modelform_factory`.

        The returned form is instantiated with `self.obj`.
        """
        if self.kwargs.get('form'):  # Custom form
            try:
                form = import_class(self.kwargs.get('form'))
            except ImportError:  # TODO: warning sent to logger or something ?
                form = modelform_factory(self.model)
        else:  # Generic form
            form = modelform_factory(self.model)
        return form(data=get_dict_from_obj(self.obj), instance=self.obj)
Beispiel #6
0
    def get_form(self):
        """Creates and returns a form on the fly from the model using
        `modelform_factory`.

        The returned form is instantiated with `self.obj`.
        """
        if self.kwargs.get('form'):  # Custom form
            try:
                form = import_class(self.kwargs.get('form'))
            except ImportError:  # TODO: warning sent to logger or something ?
                form = modelform_factory(self.model)
        else:  # Generic form
            form = modelform_factory(self.model)
        return form(data=get_dict_from_obj(self.obj), instance=self.obj)