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

Choice values are always represented as strings. #4111

Closed
unreality opened this issue May 11, 2016 · 6 comments
Closed

Choice values are always represented as strings. #4111

unreality opened this issue May 11, 2016 · 6 comments
Labels
Milestone

Comments

@unreality
Copy link

If you call OPTIONS on a model that has fields with choices, the displayed choices are always strings eg:

"account": {
    "type": "field",
    "required": true,
    "read_only": false,
    "label": "Account",
     "choices": [
        {
            "display_name": "Choice 1 Display Name",
            "value": "71518788"
        },
        {
            "display_name": "Choice 2 Display Name",
            "value": "71519217"
        }
        ....etc

Currently for RelatedField, when you request the choices property, all values are converted to strings, but this probably doesn't make sense on things such as PrimaryKeyRelatedField, or other field values which are integers or booleans.

@bdewitt1991
Copy link

Black list string

@ansumanbebarta
Copy link

May I have steps to reproduce this? I am interested to look into it. I tried to reproduce but I couldn't.

@bdewitt1991
Copy link

Open

On Jun 12, 2016 5:41 PM, "Ansuman Bebarta" notifications@github.com wrote:

May I have steps to reproduce this? I am interested to look into it. I
tried to reproduce but I couldn't.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@unreality
Copy link
Author

unreality commented Jun 14, 2016

Here is an example:

class ExampleTypeModel(models.Model):
    id = models.AutoField(primary_key=True)
    label = models.CharField(max_length=100)
    def __str__(self):
        return self.label

class ExampleModel(models.Model):
    id = models.AutoField(primary_key=True)
    example_type = models.ForeignKey('ExampleTypeModel', models.DO_NOTHING)
    name = models.CharField(max_length=100)
class ExampleModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = ExampleModel

class ExampleTypeModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = ExampleTypeModel
class ExampleModelView(viewsets.ModelViewSet):
    serializer_class = ExampleModelSerializer
    queryset = ExampleModel.objects.all()

If you add the viewset to your url router, and request OPTIONS, the choices for the foreign key (ExampleTypeModel) will all be strings.

I believe the problem is here, it looks like its always forcing it to a string value?

@ansumanbebarta
Copy link

Hi,

Thank you for the steps you have provided. I am getting following while requesting OPTIONS. But I am not getting choices for the foreign key.

{
    "name": "Example Model List",
    "description": "",
    "renders": [
        "application/json",
        "text/html"
    ],
    "parses": [
        "application/json",
        "application/x-www-form-urlencoded",
        "multipart/form-data"
    ],
    "actions": {
        "POST": {
            "id": {
                "type": "integer",
                "required": false,
                "read_only": true,
                "label": "Id"
            },
            "name": {
                "type": "string",
                "required": true,
                "read_only": false,
                "label": "Name",
                "max_length": 100
            },
            "example_type": {
                "type": "field",
                "required": true,
                "read_only": false,
                "label": "Example type"
            }
        }
    }
}

I have forked the lastest django-rest-framework repo so that I can create PR. Now after a bit of searching I found following:
#4021
Seems like we don't show choices anymore for related fields. So is it a BUG anymore?

@tomchristie
Copy link
Member

Closed, both via #4021 (Don't show in OPTIONS - thanks @ansumanbebarta!) and via #4379 (.choices property of RelatedField should preserve non-string values.)

@tomchristie tomchristie added this to the 3.4.4 Release milestone Aug 10, 2016
@tomchristie tomchristie changed the title Choice values in OPTIONS calls are always represented as strings, regardless of type Choice values are always represented as strings, regardless of type Aug 10, 2016
@tomchristie tomchristie changed the title Choice values are always represented as strings, regardless of type Choice values are always represented as strings. Aug 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants