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

Introduce ConsciousChoiceField and LazyChoiceField #158

Open
itsdkey opened this issue Mar 10, 2021 · 1 comment
Open

Introduce ConsciousChoiceField and LazyChoiceField #158

itsdkey opened this issue Mar 10, 2021 · 1 comment

Comments

@itsdkey
Copy link

itsdkey commented Mar 10, 2021

I would like to introduce 2 fields that I think can have a good impact on this repo

  1. ConsciousChoiceField

This field can be used in situations where we want to force a user to pick an option. Let's say we have a model:

class Poll:
choice = models.IntegerField(chocies=[(1, 'test'), (2, 'bar')])

and a serializer:

class PollSerializer(serializers.ModelSerializer):
choice = ConsciousChoiceField(choices=[(1, 'test'), (2, 'bar')]))

DRF by default does not insert a blank choice as an option because the model's field says that a choice must be picked. So DRF sets the widget to the available options. But in this scenario, a user can just skip the field and leave the first option that was picked. In some situations, we don't want that - we want a clear, conscious choice.

  1. LazyChoiceField

This field can be used if we want to generate choices each time a serializer is loaded. In my mind, there are lots of scenarios. Firstly: when we want to query the database to get some available options. Secondly: if we want to ask a 3rd party API for our choices. Thirdly: when we want to load some options based on a keyword argument (for example a user's age/country/postcode).

If someone has some questions I'm willing to answer them 😃

itsdkey added a commit to itsdkey/drf-extra-fields that referenced this issue Mar 10, 2021
@gokselcoban
Copy link

Hi, thank you for these fields.

Sorry, but I couldn't understand the use case of these fields. In these cases, I think we should we Django forms instead of DRF.

This package is mainly designed for JSON inputs and outputs. As far as I understand you use it as HTML.

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

No branches or pull requests

3 participants