mirror of https://github.com/snachodog/mybuddy.git
Add a custom serializer for core models.
This serializer associates the core models with their related child fields and runs model clean() methods during POST requests.
This commit is contained in:
parent
2f21e3d7f4
commit
ed84f950bc
|
@ -8,6 +8,20 @@ from django.contrib.auth.models import User
|
||||||
from core import models
|
from core import models
|
||||||
|
|
||||||
|
|
||||||
|
class CoreModelSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
|
"""
|
||||||
|
Provide the child link (used by most core models) and run model clean()
|
||||||
|
methods during POST operations.
|
||||||
|
"""
|
||||||
|
child = serializers.PrimaryKeyRelatedField(
|
||||||
|
queryset=models.Child.objects.all())
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
instance = self.Meta.model(**attrs)
|
||||||
|
instance.clean()
|
||||||
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
|
@ -17,62 +31,50 @@ class UserSerializer(serializers.ModelSerializer):
|
||||||
class ChildSerializer(serializers.HyperlinkedModelSerializer):
|
class ChildSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Child
|
model = models.Child
|
||||||
fields = ('first_name', 'last_name', 'birth_date', 'slug')
|
fields = ('id', 'first_name', 'last_name', 'birth_date', 'slug')
|
||||||
lookup_field = 'slug'
|
lookup_field = 'slug'
|
||||||
|
|
||||||
|
|
||||||
class DiaperChangeSerializer(serializers.HyperlinkedModelSerializer):
|
class DiaperChangeSerializer(CoreModelSerializer):
|
||||||
child = ChildSerializer()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.DiaperChange
|
model = models.DiaperChange
|
||||||
fields = ('child', 'time', 'wet', 'solid', 'color')
|
fields = ('id', 'child', 'time', 'wet', 'solid', 'color')
|
||||||
|
|
||||||
|
|
||||||
class FeedingSerializer(serializers.HyperlinkedModelSerializer):
|
class FeedingSerializer(CoreModelSerializer):
|
||||||
child = ChildSerializer()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Feeding
|
model = models.Feeding
|
||||||
fields = ('child', 'start', 'end', 'duration', 'type', 'method',
|
fields = ('id', 'child', 'start', 'end', 'duration', 'type', 'method',
|
||||||
'amount')
|
'amount')
|
||||||
|
|
||||||
|
|
||||||
class NoteSerializer(serializers.HyperlinkedModelSerializer):
|
class NoteSerializer(CoreModelSerializer):
|
||||||
child = ChildSerializer()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Note
|
model = models.Note
|
||||||
fields = ('child', 'note', 'time')
|
fields = ('id', 'child', 'note', 'time')
|
||||||
|
|
||||||
|
|
||||||
class SleepSerializer(serializers.HyperlinkedModelSerializer):
|
class SleepSerializer(CoreModelSerializer):
|
||||||
child = ChildSerializer()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Sleep
|
model = models.Sleep
|
||||||
fields = ('child', 'start', 'end', 'duration')
|
fields = ('id', 'child', 'start', 'end', 'duration')
|
||||||
|
|
||||||
|
|
||||||
class TimerSerializer(serializers.HyperlinkedModelSerializer):
|
class TimerSerializer(CoreModelSerializer):
|
||||||
user = UserSerializer()
|
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Timer
|
model = models.Timer
|
||||||
fields = ('name', 'start', 'end', 'duration', 'active', 'user')
|
fields = ('id', 'name', 'start', 'end', 'duration', 'active', 'user')
|
||||||
|
|
||||||
|
|
||||||
class TummyTimeSerializer(serializers.HyperlinkedModelSerializer):
|
class TummyTimeSerializer(CoreModelSerializer):
|
||||||
child = ChildSerializer()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.TummyTime
|
model = models.TummyTime
|
||||||
fields = ('child', 'start', 'end', 'duration', 'milestone')
|
fields = ('id', 'child', 'start', 'end', 'duration', 'milestone')
|
||||||
|
|
||||||
|
|
||||||
class WeightSerializer(serializers.HyperlinkedModelSerializer):
|
class WeightSerializer(CoreModelSerializer):
|
||||||
child = ChildSerializer()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Weight
|
model = models.Weight
|
||||||
fields = ('child', 'weight', 'date')
|
fields = ('id', 'child', 'weight', 'date')
|
||||||
|
|
Loading…
Reference in New Issue