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

How to include choices for ResourceRelatedField? #701

Closed
arthur-s opened this issue Sep 17, 2019 · 3 comments
Closed

How to include choices for ResourceRelatedField? #701

arthur-s opened this issue Sep 17, 2019 · 3 comments

Comments

@arthur-s
Copy link

I need to get choices for ResourceRelatedField, when I make OPTIONS request. How to do it?

# serializer:
self.fields['members'] = ResourceRelatedField(
    queryset=Profile.objects.filter(is_active=True),
    many=True
)

Current output:

                "members": {
                    "type": "Relationship",
                    "relationship_type": "ManyToMany",
                    "relationship_resource": "profiles",
                    "required": false,
                    "read_only": false,
                    "write_only": false,
                    "label": "Members",
                    "initial": "[]"
                },

If I remove this line, (It is what I want) I get desired output:

                "members": {
                    "type": "field",
                    "required": false,
                    "read_only": false,
                    "label": "Members",
                    "relationship_type": "ManyToMany",
                    "choices": [
                        {
                            "value": "{\"type\": \"profiles\", \"id\": \"1\"}",
                            "display_name": "Arthur"
                        },
                        {
                            "value": "{\"type\": \"profiles\", \"id\": \"2\"}",
                            "display_name": "Tom"
                        }
                    ]
                },

Can we remove not field_info.get('relationship_resource') and from choices condition, or how to generate output that I want (without modifying metadata.py)?

@arthur-s
Copy link
Author

I can add include_choices attribute to ResourceRelatedField and create PR.

@sliverc
Copy link
Member

sliverc commented Sep 17, 2019

DJA excludes choices as Django REST Framework does as well.

See encode/django-rest-framework#3751 encode/django-rest-framework#4021 for more details why this is.

As of now it seems the way to include choices for related fields in Django REST Framework is a custom metadata class - see encode/django-rest-framework#4021 (comment)

As DJA we try to follow Django REST Framework standards as much as possible. So in this case if you think an option include_choices would be a good idea you would need to request such feature upstream. Once it is applied we can then follow up with the same option in DJA as well.

@arthur-s
Copy link
Author

Closing this issue, since they recommend use custom meta class.

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

2 participants