serializers.py 1.44 KB
Newer Older
1 2 3
"""
Serializers to be used in APIs.
"""
4

5
from rest_framework import serializers
6 7 8 9 10 11 12 13 14 15 16 17 18 19


class CollapsedReferenceSerializer(serializers.HyperlinkedModelSerializer):
    """Serializes arbitrary models in a collapsed format, with just an id and url."""
    url = serializers.HyperlinkedIdentityField(view_name='')

    def __init__(self, model_class, view_name, id_source='id', lookup_field=None, *args, **kwargs):
        """Configures the serializer.

        Args:
            model_class (class): Model class to serialize.
            view_name (string): Name of the Django view used to lookup the
                model.
            id_source (string): Optional name of the id field on the model.
20 21
                Defaults to 'id'. Also used as the property name of the field
                in the serialized representation.
22 23 24 25 26 27 28 29 30 31 32
            lookup_field (string): Optional name of the model field used to
                lookup the model in the view. Defaults to the value of
                id_source.
        """
        if not lookup_field:
            lookup_field = id_source

        self.Meta.model = model_class

        super(CollapsedReferenceSerializer, self).__init__(*args, **kwargs)

33
        self.fields[id_source] = serializers.CharField(read_only=True)
34 35
        self.fields['url'].view_name = view_name
        self.fields['url'].lookup_field = lookup_field
36
        self.fields['url'].lookup_url_kwarg = lookup_field
37 38

    class Meta(object):
39
        fields = ("url",)