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

Question: Using Autocomplete widget in FieldBlock #76

Open
bmihelac opened this issue Aug 4, 2020 · 0 comments
Open

Question: Using Autocomplete widget in FieldBlock #76

bmihelac opened this issue Aug 4, 2020 · 0 comments

Comments

@bmihelac
Copy link

bmihelac commented Aug 4, 2020

Hello everyone,

Is it supported to use Autocomplete widget in FieldBlock?

I have successfully created it but am not sure if is supported, recommended or best practice.

Code:

class CountryAutocomplete(Autocomplete):
    target_model = Country
    can_create = False
    is_single = False


class CountriesBlock(FieldBlock):
    def __init__(self, required=True, help_text=None, **kwargs):
        self.field = forms.ModelMultipleChoiceField(
            Country.objects.all(), widget=CountryAutocomplete
        )
        super().__init__(**kwargs)

    def get_prep_value(self, value):
        if value is None:
            return None
        return [obj.pk for obj in value]


class VideoBlock(StructBlock):
    countries = CountriesBlock("countries")

If it is, maybe it would be good idea add example to documentation.

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

1 participant