Re: Usage of read_only=True with default=serializers.CurrentUserDefault() in 3.8.2

1,406 views
Skip to first unread message

Carlton Gibson

unread,
Apr 12, 2018, 2:36:33 AM4/12/18
to django-res...@googlegroups.com
Hi Stuart. 

This was a breaking change in v3.8. It was necessary in order to support dotted-sources with nullable fields. (The tl;dr is that having the default set for read-only fields in these cases led to unavoidable inconsistencies, so we had to change it.)

You now need to pass the value of your read-only field when calling save. In your case, something like: 

    def perform_create(self, serializer):
        serializer.save(uploaded_by=self.request.user)

Ultimately the new approach is more consistent. The use of defaults for read-only fields was always a little strange. 

This is mentioned in the 3.8 Release Announcement (and the Release Notes)


For **validation** you still need to set the default value as you always have. (This was broken in v3.8.0 but fixed in v3.8.2.) 

Thus this is still correct: 

    default=serializers.CurrentUserDefault()

read-only fields that are part of a unique_together constraint still need some value to validate against, and the default plays that role. 

I hope that explains it. 

Since we’ve made changes here it may be that the docs could do with an adjustment. I think they’re still correct but maybe drawing this out better it needed. It would be good if you could review and offer any suggestions in a PR or Issue on the repo if you think that’s appropriate. 

Kind Regards,

Carlton


On 11 Apr 2018, at 23:30, Stuart George <stu...@accentdesign.co.uk> wrote:

Hi

Ive been using the following with no issues for months, to set the default user and allow it to be sent out in the get requests:

class FileSerializer(serializers.ModelSerializer):
    uploaded_by 
= serializers.PrimaryKeyRelatedField(
        read_only
=True,
        
default=serializers.CurrentUserDefault()
    
)

    
class Meta:
        model 
= File
        fields 
= ...

Its used in a basic ModelViewSet and the uploaded_by is not part of any unique validators, it just required.

I just want to check it this is still ok to do this as the uploaded_by is correctly in the serializers data but is missing from the validated_data when its reported as being valid.
Then the save attempt as expected wines with IntegrityError: null value in column "uploaded_by_id" violates not-null constraint.

I can see that there was a unique together validation went in 3.8.2 for read_only + default. Im guessing this is the cause but wanted to check what im doing is still sensible.

Cheers Stu

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stuart George

unread,
Apr 13, 2018, 7:02:31 PM4/13/18
to django-res...@googlegroups.com
Hi Carlton,

Yeh thanks I saw the release notes 5 mins after I posted this then deleted the question. 

Thanks for responding, Stu

To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-- 
Stuart George
Senior Web Developer

Accent Design Group Ltd
The Studio, Adam & Eve Yard
94B St Benedicts Street, Norwich NR2 4AB

Skype: accent.stuart

Company Number: 4618709, Registered in: England & Wales.
Reply all
Reply to author
Forward
0 new messages