Skip to content

add read_only check for dotted-source fields in serializers #2268

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

Closed
wants to merge 2 commits into from
Closed

add read_only check for dotted-source fields in serializers #2268

wants to merge 2 commits into from

Conversation

leahoswald
Copy link

The error message tells that you should create an own crate() function
or set read_only=True for the serializer field. The Problem is that the check
ignores this read_only flag.

Ben Oswald added 2 commits December 13, 2014 18:09
The error message tells that you should create an own crate() function
or set read_only=True for the field. The Problem is that the check
ignores the read_only flag.
@tomchristie
Copy link
Member

We check 'key in validated_data' so we are checking if it's being written to. Closing this off given that, but happy to reopen if you can explain the motivation further in light of that. Eg do you have an example failure case. Also note #2266 which went into master today, and improves the robustness of the check (may be relevant to whatever you're seeing?)

@leahoswald
Copy link
Author

It looks like #2266 fixed the problem for me. It happened in an usecase similar to this the one described in the Tutorial http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/#associating-snippets-with-users

Model:

class CategoryViewSet(NestedViewSetMixin, viewsets.ModelViewSet):               
      queryset = Category.objects.all()                                           
      serializer_class = CategorySerializer                                       
      def perform_create(self, serializer):                                       
         serializer.save(user = self.request.user) 

Serializer:

class CategorySerializer(serializers.ModelSerializer):                          
     user = serializers.ReadOnlyField( source='user.id')          
     class Meta:                                                                 
          model = Category                                                        
          fields = ('id', 'user', 'title')

This was the traceback: http://dpaste.com/0FT2MY1

@tomchristie
Copy link
Member

Great. Probably worth us rolling 3.0.2 reasonably soon then.

@tomchristie tomchristie modified the milestone: 3.0.2 Release Dec 13, 2014
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