Skip to content

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
@lovelydinosaur
Copy link
Contributor

Fantastic, thanks!

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