Skip to content

Not calling function with source= anymore #4602

Closed

Description

@debnet

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. ;)

Activity

  1. added this to the 3.5.1 Release milestone on Oct 20, 2016
  2. lovelydinosaur commented on Oct 21, 2016

    @lovelydinosaur
    Member

    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?

  3. guinner commented on Oct 24, 2016

    @guinner
    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

  4. rpkilby commented on Oct 24, 2016

    @rpkilby
    Contributor

    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.

  5. rpkilby commented on Oct 24, 2016

    @rpkilby
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions