Implementing ListSerializer update for ModelSerializer with HyperlinkedIdentityField

839 views
Skip to first unread message

William Shallum

unread,
Sep 13, 2015, 5:47:23 AM9/13/15
to django-res...@googlegroups.com
Hi,

I am trying to implement an update method for a ListSerializer. The
ListSerializer is for a ModelSerializer which has a
HyperlinkedIdentityField.

The problem I have is since the HyperlinkedIdentityField is read-only,
it does not add to validated_data, so I cannot distinguish between new
objects (without URL) and existing objects (with URL of existing
object). The example in the docs (
http://www.django-rest-framework.org/api-guide/serializers/#customizing-multiple-update
) uses an id attribute that is sent, but I can't get at the
id-equivalent attribute here.

Any tips?

Regards,
William

Antonis Kalipetis

unread,
Sep 15, 2015, 1:44:23 AM9/15/15
to Django REST framework
Hello William,

I would suggest that the best way would be to use a ListField[1], which you would declare write-only. Then, you'd use this field for getting a list for IDs for the related resources and creating or updating your model, while continue having your HyperlinkedIdentityField for rendering.

For example, changing the example given for HyperlinkedIdentityField[2]:

class AlbumSerializer(serializers.HyperlinkedModelSerializer):
    track_listing
= serializers.HyperlinkedIdentityField(view_name='track-list')
    track_list_ids
serializers.ListField(
        child=serializers.IntegerField(min_value=0, max_value=100), write_only=True
    )


   
class Meta:
        model
= Album
        fields
= ('album_name', 'artist', 'track_listing', 'track_list_ids')


Then, you'll be able to update your model using the reference you provided and find the IDs in the new `track_list_ids` field.

ak

William Shallum

unread,
Sep 16, 2015, 10:38:00 AM9/16/15
to Django REST framework
Hi Antonis,

I have worked around this by creating a write-only field that peeks at the "url" field of the dictionary of data:

class WriteOnlySynonymField(serializers.Field):

   
def __init__(self, **kwargs):
        kwargs
['default'] = serializers.empty
        kwargs
['required'] = False
        kwargs
['write_only'] = True
       
self.synonym_for = kwargs.pop('synonym_for')
       
super(WriteOnlySynonymField, self).__init__(**kwargs)

   
def get_value(self, dictionary):
       
return dictionary.get(self.synonym_for, serializers.empty)

   
def to_internal_value(self, data):
       
return data

And it is used as:

submitted_url = WriteOnlySynonymField(synonym_for='url')


Regards,
William

Antonis Kalipetis

unread,
Sep 17, 2015, 4:59:56 AM9/17/15
to Django REST framework
Great William, that should work too.

ak

--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/Z_cfZRm0qXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages