Skip to content

Introduce ConsciousChoiceField and LazyChoiceField #158

@itsdkey

Description

@itsdkey

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 😃

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions