Skip to content

update SerializerMethodField example in docs #7858

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

Merged
merged 2 commits into from
Mar 22, 2021

Conversation

afolksetapart
Copy link
Contributor

Note: Before submitting this pull request, please review our contributing guidelines.

Description

This PR will update the code in the example below (as seen in the docs), which will throw the following error if run:

AssertionError: ("Creating a ModelSerializer without either the 'fields' attribute or the 'exclude' attribute has been deprecated since 3.3.0, and is now disallowed. Add an explicit fields = 'all' to the AlbumSerializer serializer.",)

from django.contrib.auth.models import User
from django.utils.timezone import now
from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):
    days_since_joined = serializers.SerializerMethodField()

    class Meta:
        model = User

    def get_days_since_joined(self, obj):
        return (now() - obj.date_joined).days 

This PR adds the required fields = '__all__' to the example and may help folks following along for the first time.

@afolksetapart afolksetapart changed the title update SerializerMethodField example update SerializerMethodField example in docs Mar 21, 2021
@tomchristie
Copy link
Member

Fantastic, thanks!

@tomchristie tomchristie merged commit 71e6c30 into encode:master Mar 22, 2021
@afolksetapart
Copy link
Contributor Author

Thanks @tomchristie !

stefanacin pushed a commit to stefanacin/django-rest-framework that referenced this pull request Mar 22, 2021
* update SerializerMethodField example

* fix formatting
sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
* update SerializerMethodField example

* fix formatting
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

Successfully merging this pull request may close these issues.

2 participants