-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
ChoiceField metadata does not support grouped choices. #3101
Comments
The flat list of choices would be my preferred option here. |
@tomchristie I was hoping you would go for the hierarchical one as I need the hierarchy in an application consuming the api. If the choices are flat, then how would you recommend we get the hierarchy from the api. My first idea would be to extend |
Exactly so, yes. |
Related to #1533 |
Regression test for encode#3101
Rolling this into #1533. |
@maxpeterson @tomchristie I also need the nested/grouped representation on to be returned by the meta for an app. Can you guys perhaps link me to an example of hot to do this? |
@wernerhp I have achieved this by adding an additional
|
@maxpeterson Thanks for the snippet. I'm still battling a bit.
Working with the Django example REST_FRAMEWORK = {
...
'DEFAULT_METADATA_CLASS': 'custom.metadata.GroupedChoiceMetadata',
...
} class Media(models.Model):
MEDIA_CHOICES = (
('Audio', (
('vinyl', 'Vinyl'),
('cd', 'CD'),
)
),
('Video', (
('vhs', 'VHS Tape'),
('dvd', 'DVD'),
)
),
('unknown', 'Unknown'),
)
type = models.CharField(max_length=255, choices=MEDIA_CHOICES) class MediaSerializer(serializers.ModelSerializer):
class Meta:
fields = [
'type',
]
model = Media printing out |
@wernerhp try using field.grouped_choices - Sorry about that |
To clarify try ...
|
After some effort I found that class GroupedChoiceMetadata(SimpleMetadata):
def get_field_info(self, field):
field_info = super(GroupedChoiceMetadata, self).get_field_info(field)
if field_info.get('type') is 'choice':
field_info['grouped_choices']= field.grouped_choices
return field_info I wonder why @tomchristie Do you perhaps have some insights for me? |
@wernerhp - Mostly likely simply historical. Expect they didn't exist when we added the metadata API. |
If a serialiser has a ChoiceField with grouped choices:
The
OPTIONS
metadata for the field includes thechoices
:I am not certain what the behaviour should be, but something including the nested choices would be useful:
If this is not appropriate then a flat list of choices might be appropriate.
The text was updated successfully, but these errors were encountered: