Created
April 5, 2014 00:19
-
-
Save jpotts18/9985611 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import User | |
from api.models import Lock, Profile | |
from rest_framework import serializers | |
class UserSerializer(serializers.HyperlinkedModelSerializer): | |
class Meta: | |
model = User | |
fields = ('url', 'username', 'email', 'groups') | |
class LockSerializer(serializers.ModelSerializer): | |
# The source argument controls which attribute is used to populate a field, | |
# and can point at any attribute on the serialized instance. | |
# It can also take the dotted notation in which case it will traverse the given attributes. | |
user_id = serializers.Field(source='profile.user.id') | |
# A serializer is necessary to make a relationship with profiles | |
# when a lock is posted with a profile_id | |
>>> | |
profile = ProfileSerializer | |
<<< | |
# TODO: how to show entire profile object | |
class Meta: | |
model = Lock | |
fields = ( | |
'id', | |
'name', | |
'serial_num', | |
'pre_shared_key', | |
'status', | |
'type', | |
'description', | |
'is_active', | |
'user_id', | |
# 'profile', | |
) | |
class ProfileSerializer(serializers.ModelSerializer): | |
>>> | |
locks = LockSerializer(many=True) | |
<<< | |
class Meta: | |
model = Profile | |
fields = ( | |
'first_name', | |
'last_name', | |
'bio', | |
'avatar_url', | |
'phone', | |
'user', | |
'created', | |
'updated', | |
'locks', | |
'is_active', | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment