django-better-admin-arrayfield icon indicating copy to clipboard operation
django-better-admin-arrayfield copied to clipboard

Hi file fields as well as choices in charfields dont work properly

Open kaustubhamrit opened this issue 6 years ago • 2 comments

  • Django better admin ArrayField version:
  • Django version:
  • Python version:
  • Operating System:

Description

Describe what you were trying to get done. Tell us what happened, what went wrong, and what you expected to happen.

What I Did

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.

kaustubhamrit avatar Dec 18 '19 14:12 kaustubhamrit

Hello, That would be great to support Choice widget such as:

test = ArrayField(
        models.IntegerField(
            choices=constants.SOMETHING
        ),
        null=True
)

sylvainblot avatar May 01 '20 09:05 sylvainblot

I tried to implement a subclass of DynamicArrayWidget for the case where the subwidget is forms.Select:

class DynamicArraySelectWidget(DynamicArrayWidget):
    def __init__(self, *args, **kwargs):
        kwargs['subwidget_form'] = forms.Select
        super().__init__(*args, **kwargs)

    def get_context(self, name, value, attrs):
        context_value = value or ['']
        context = super().get_context(name, context_value, attrs)
        final_attrs = context['widget']['attrs']
        context['widget']['is_none'] = value is None

        sw = []
        for index, item in enumerate(context['widget']['value']):
            widget_attrs = final_attrs.copy()
            test_choices = [ (n, n) for n in ('foo', 'bar', 'baz') ]
            widget = self.subwidget_form(choices=test_choices)
            widget.is_required = self.is_required
            sw.append(widget.get_context(name, item, widget_attrs)['widget'])

        context['widget']['subwidgets'] = sw
        return context

I found that the form itself works, but the "add another" button is broken, because the javascript assumes that the subwidgets will be input elements. That's as far as I got with it - for now I'm going to list valid choices in the help_text, because validation of choices is working, at least, with the text input.

@gradam, any thoughts on choices?

johncronan avatar Feb 18 '22 21:02 johncronan