Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looking at the Django REST framework (DRF) source I settled on the view that a DRF serializer is strongly tied to an accompanying Model for unserializing purposes. <code>Field</code>'s <code>source</code> param make this less so for serializing purposes.</p> <p>With that in mind, and viewing serializers as encapsulating validation and save behaviour (in addition to their (un)serializing behaviour) I used two serializers: one for each of the User and UserProfile models:</p> <pre><code>class UserSerializer(serializer.ModelSerializer): class Meta: model = User fields = ['email',] class UserProfileSerializer(serializer.ModelSerializer): email = serializers.EmailField(source='user.email') class Meta: model = UserProfile fields = ['id', 'favourite_number', 'email',] </code></pre> <p>The <code>source</code> param on the <code>EmailField</code> handles the serialization case adequately (e.g. when servicing GET requests). For unserializing (e.g. when serivicing PUT requests) it is necessary to do a little work in the view, combining the validation and save behaviour of the two serializers:</p> <pre><code>class UserProfileRetrieveUpdate(generics.GenericAPIView): def get(self, request, *args, **kwargs): # Only UserProfileSerializer is required to serialize data since # email is populated by the 'source' param on EmailField. serializer = UserProfileSerializer( instance=request.user.get_profile()) return Response(serializer.data) def put(self, request, *args, **kwargs): # Both UserProfileSerializer and UserProfileSerializer are required # in order to validate and save data on their associated models. user_profile_serializer = UserProfileSerializer( instance=request.user.get_profile(), data=request.DATA) user_serializer = UserSerializer( instance=request.user, data=request.DATA) if user_profile_serializer.is_valid() and user_serializer.is_valid(): user_profile_serializer.save() user_serializer.save() return Response( user_profile_serializer.data, status=status.HTTP_200_OK) # Combine errors from both serializers. errors = dict() errors.update(user_profile_serializer.errors) errors.update(user_serializer.errors) return Response(errors, status=status.HTTP_400_BAD_REQUEST) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload