python - Get the chosen values from FilteredSelectMultiple widget in Django -
in django admin , know how can chosen values filteredselectmultiple widget in, when form saved?
class controllerform(forms.modelform): terminal = forms.modelmultiplechoicefield(queryset=[]) def __init__(self, *args, **kwargs): super(controllerform, self).__init__(*args, **kwargs) self.fields['terminal'].widget = widgets.filteredselectmultiple('terminals', false) self.fields['terminal'].help_text = "select terminals added group." self.fields['terminal'].required = false self.fields['terminal'].label = "select terminal(s)" self.fields['terminal'].choices = [(t.id, str(t)) t in terminal.objects.filter(associated=false)] class meta: model = controller class controlleradmin(admin.modeladmin): """ controller admin form customization. """ list_display = ('name', 'group',) form = controllerform admin.site.register(controller, controlleradmin)
edit: think can access values in save_model method. (https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.modeladmin.save_model)
i've reached solution. using save_model method available in modeladmin 1 can access chosen objects in form.cleaned_data dictionary.
def save_model(self, request, obj, form, change): terminal in form.cleaned_data['terminal']: ... obj.save()
checkout https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.modeladmin.save_model , https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs more details on method.
thanks
python django forms widget admin
No comments:
Post a Comment