Commit 052026e3 authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

automatically use CheckboxSelectMultiple for M2M

parent af906a75
...@@ -223,15 +223,19 @@ def account_detail(request): ...@@ -223,15 +223,19 @@ def account_detail(request):
else: else:
is_posted = request.method == 'POST' is_posted = request.method == 'POST'
pref = klass.get_for_user(user=request.user) pref = klass.get_for_user(user=request.user)
widgets_dict = dict(
(f.name, widgets.CheckboxSelectMultiple()) for f in pref._meta.get_fields() if f.many_to_many)
form_prefs = forms.modelform_factory( form_prefs = forms.modelform_factory(
klass, klass,
fields=list(klass.get_allowed_fields()), fields=list(pref.get_allowed_fields()),
widgets=widgets_dict,
)(instance=pref, data=request.POST if is_posted else None) )(instance=pref, data=request.POST if is_posted else None)
if is_posted and form_prefs.is_valid(): if is_posted and form_prefs.is_valid():
form_prefs.save() form_prefs.save()
form_prefs = forms.modelform_factory( form_prefs = forms.modelform_factory(
klass, klass,
fields=list(klass.get_allowed_fields()), fields=list(pref.get_allowed_fields()),
widgets=widgets_dict,
)(instance=klass.get_for_user(user=request.user)) )(instance=klass.get_for_user(user=request.user))
# dirty patch for time field (don't know why django doesn't express the correct type by default) # dirty patch for time field (don't know why django doesn't express the correct type by default)
for f in form_prefs.fields.values(): for f in form_prefs.fields.values():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment