Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change the widget of the field? #117

Open
alokRamteke opened this issue Jun 12, 2020 · 1 comment
Open

How to change the widget of the field? #117

alokRamteke opened this issue Jun 12, 2020 · 1 comment

Comments

@alokRamteke
Copy link

Right now, by default we have CheckboxSelectMultiple widget for the MultiSelectField.
How can I change it to SelectMultiple and show it by default?
Do I need to pass any parameter in model-field?

Right now, I am following below method to override it in the models. Let me know, if we have any better way?

from django.utils.text import capfirst
from multiselectfield import MultiSelectField
from multiselectfield.forms.fields import MultiSelectFormField

class MultiSelectDropdownFormField(MultiSelectFormField):
    widget = forms.SelectMultiple


class MultiSelectDropdownField(MultiSelectField):
    def formfield(self, **kwargs):
        defaults = {'required': not self.blank,
                    'label': capfirst(self.verbose_name),
                    'help_text': self.help_text,
                    'choices': self.choices,
                    'max_length': self.max_length,
                    'max_choices': self.max_choices}
        if self.has_default():
            defaults['initial'] = self.get_default()
        defaults.update(kwargs)
        return MultiSelectDropdownFormField(**defaults)


field = MultiSelectDropdownField(max_length=30, choices=CHOICES)
@LukasKlement
Copy link

I've just written a comment on how to display a field using the multi select field in the Django Admin using the PostgreSQL ArrayField: #124

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants