EventSpecificSerializer provides event attribute

This commit is contained in:
Qwann 2017-07-25 18:47:50 +02:00
parent 771fdf878e
commit d19e5978b6

View file

@ -3,14 +3,28 @@ from rest_framework import serializers
from event.models import Event, ActivityTag, Place, ActivityTemplate 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 # Classes utilitaires
class EventSpecificSerializer(serializers.ModelSerializer): class EventSpecificSerializer(serializers.ModelSerializer):
""" """
Provide `update` and `create` methods for nested view with an Event 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 the event id has to be provided in the `save` method
Works fine with view.EventSpecificViewSet 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): def update(self, instance, validated_data):
""" """
Note : does NOT change the event value of the instance Note : does NOT change the event value of the instance
@ -30,19 +44,8 @@ class EventSpecificSerializer(serializers.ModelSerializer):
# Serializers # 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 # TODO rajouter des permissions
class PlaceSerializer(EventSpecificSerializer): class PlaceSerializer(EventSpecificSerializer):
event = EventSerializer(allow_null=True, read_only=True)
class Meta: class Meta:
model = Place model = Place
fields = ('url', 'id', 'name', 'description', 'event') fields = ('url', 'id', 'name', 'description', 'event')
@ -50,8 +53,6 @@ class PlaceSerializer(EventSpecificSerializer):
# TODO rajouter des permissions # TODO rajouter des permissions
class ActivityTagSerializer(EventSpecificSerializer): class ActivityTagSerializer(EventSpecificSerializer):
event = EventSerializer(allow_null=True, read_only=True)
class Meta: class Meta:
model = ActivityTag model = ActivityTag
fields = ('url', 'id', 'name', 'is_public', 'color', 'event') fields = ('url', 'id', 'name', 'is_public', 'color', 'event')