python - Django formset - Change field type -
i have parameter model looks like:
class parameter(models.model): spot = models.foreignkey(spot, related_name = 'spot_parameters', verbose_name = _("spot")) parameter = models.foreignkey(parametertemplate, verbose_name = _("parameter")) value = models.charfield(max_length = 255, db_index = true, verbose_name = _("value")) is_auto_generated = models.booleanfield(default = false, verbose_name = _("is auto generated"))
and parametertemplate model:
class parametertemplate(positionmixin): data_type_choice = ( ('int', "int"), ('decimal', "decimal"), ('text', "text"), ('list', "list"), ('size', "size"), ('boolean', "boolean"), ) name = models.charfield(max_length = 255, verbose_name = _("name")) data_type = models.charfield( max_length=50, choices=data_type_choice, db_index=true, verbose_name=_("data type") ) values = models.charfield(max_length = 255, blank = true, null = true, verbose_name = _("value list")) glyph_class = models.charfield(max_length = 255, blank = true, null = true, verbose_name = _("icon"))
i have problem create form.
for eg if data_type_choice boolean parameter want show in form checkbox, instead of textinput
@edit
parameter form views
@login_required @secure_required def account_spot_parameters_change(request, spot_id): spot = get_object_or_404(spot, id = spot_id, user = request.user)
title = _("spot parameters") parameterformset = inlineformset_factory(spot, parameter, form = parameterform, = 0) if request.method == 'post': formset = parameterformset(request.post, instance = spot) if formset.is_valid(): formset.save() formset = none messages.success(request, _("your changes have been saved.")) else: messages.error(request, _("form not filled correctly. please check highlighted fields.")) else: formset = parameterformset(instance = spot) extra_context = { 'spot': spot, 'title': title, 'formset': formset, 'mode': 'parameters', } return shortcuts.render(request, 'account_spot_change.html', extra_context)
parameterform
class parameterform(forms.modelform): class meta: model = parameter fields = ('parameter', 'value') def __init__(self, *args, **kwargs): super(parameterform, self).__init__(*args, **kwargs) add_widget_css_class(self, 'form-control')
template
% form in formset %} {% field in form.hidden_fields %} {{ field }} {% endfor %} <div class="form-group"> <div class="col-sm-4 col-sm-offset-2"> {{ form.parameter }} </div> <div class="col-sm-3"> {{ form.value }} </div> <div class="col-sm-1 text-center"> {{ form.delete }} </div> </div> {% endfor %}
Comments
Post a Comment