From d19e5978b6ba6e46b8f0cacc5f98c7ff26d70bc8 Mon Sep 17 00:00:00 2001 From: Qwann Date: Tue, 25 Jul 2017 18:47:50 +0200 Subject: [PATCH] EventSpecificSerializer provides event attribute --- api/event/serializers.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/api/event/serializers.py b/api/event/serializers.py index e898d8d..183c39d 100644 --- a/api/event/serializers.py +++ b/api/event/serializers.py @@ -3,14 +3,28 @@ from rest_framework import serializers from event.models import Event, ActivityTag, Place, ActivityTemplate +# Event Serializer +class EventSerializer(serializers.HyperlinkedModelSerializer): + created_by = serializers.ReadOnlyField(source='created_by.username') + + class Meta: + model = Event + fields = ('url', 'id', 'title', 'slug', 'created_by', 'creation_date', + 'description', 'beginning_date', 'ending_date') + + # Classes utilitaires class EventSpecificSerializer(serializers.ModelSerializer): """ Provide `update` and `create` methods for nested view with an Event - For example for Models which exetends EventSpecificMixin + For example for Models which extends EventSpecificMixin the event id has to be provided in the `save` method Works fine with view.EventSpecificViewSet + Also provides : + event = eventserializer(allow_null=true, read_only=true) """ + event = EventSerializer(allow_null=True, read_only=True) + def update(self, instance, validated_data): """ Note : does NOT change the event value of the instance @@ -30,19 +44,8 @@ class EventSpecificSerializer(serializers.ModelSerializer): # Serializers -class EventSerializer(serializers.HyperlinkedModelSerializer): - created_by = serializers.ReadOnlyField(source='created_by.username') - - class Meta: - model = Event - fields = ('url', 'id', 'title', 'slug', 'created_by', 'creation_date', - 'description', 'beginning_date', 'ending_date') - - # TODO rajouter des permissions class PlaceSerializer(EventSpecificSerializer): - event = EventSerializer(allow_null=True, read_only=True) - class Meta: model = Place fields = ('url', 'id', 'name', 'description', 'event') @@ -50,8 +53,6 @@ class PlaceSerializer(EventSpecificSerializer): # TODO rajouter des permissions class ActivityTagSerializer(EventSpecificSerializer): - event = EventSerializer(allow_null=True, read_only=True) - class Meta: model = ActivityTag fields = ('url', 'id', 'name', 'is_public', 'color', 'event')