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

Not calling function with source= anymore #4602

Closed
debnet opened this issue Oct 20, 2016 · 4 comments
Closed

Not calling function with source= anymore #4602

debnet opened this issue Oct 20, 2016 · 4 comments

Comments

@debnet
Copy link

debnet commented Oct 20, 2016

Here the serializer:

class IndividuSerializer(ModelSerializer):
    # ...
    sexe_display = CharField(source='get_sexe_display', label='sexe', read_only=True)

The API doesn't display the choice values from Django models anymore.

{
    "url": "http://localhost:8000/api/dsn/individu/1/",
    "id": 1,
    "metadatas": null,
    "sexe_display": "<bound method curry.<locals>._curried of <Individu: XXX XXX>>",
    "...": "..."
}

In the previous release it worked just fine. ;)

@tomchristie tomchristie added this to the 3.5.1 Release milestone Oct 20, 2016
@tomchristie
Copy link
Member

tomchristie commented Oct 21, 2016

I'm not able to replicate this.

For example...

class UserSerializer(serializers.ModelSerializer):
    username = serializers.CharField(source='get_username', read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'email')

Results in this...

screen shot 2016-10-21 at 15 40 44

Could you try to reduce this to a reproducible test case, or similar?

@guinner
Copy link

guinner commented Oct 24, 2016

class IndividuSerializer(ModelSerializer):
    # ...
    sexe_display = CharField(source='get_sexe_display', label='sexe', read_only=True)

The field sexe is defined in django model. And The method get_sexe_display is django's _get_FIELD_display. It's returned <bound method curry.<locals>._curried of <Individu: XXX XXX>>.

_curried(*moreargs, **morekwargs) is defined in django.utils.functional

But In drf 'fields.py' line 62, method is_simple_callable, param.default is always param.empty.
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py#L62

rpkilby added a commit to rpkilby/django-rest-framework that referenced this issue Oct 24, 2016
@rpkilby
Copy link
Member

rpkilby commented Oct 24, 2016

I've started a patch which is failing - will have to come back to this later.

The problem with the patch implementation is that it can't determine if _args and *_kwargs are safely callable.

@rpkilby
Copy link
Member

rpkilby commented Oct 24, 2016

For those in need of a quick fix for get_FIELD_display(), you can add a FIELD_display property that simply calls the method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants