You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to introduce 2 fields that I think can have a good impact on this repo
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.
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 😃
The text was updated successfully, but these errors were encountered:
itsdkey
added a commit
to itsdkey/drf-extra-fields
that referenced
this issue
Mar 10, 2021
I would like to introduce 2 fields that I think can have a good impact on this repo
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.
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 😃
The text was updated successfully, but these errors were encountered: