Skip to content

Instantly share code, notes, and snippets.

@jpotts18
Created April 5, 2014 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpotts18/9985611 to your computer and use it in GitHub Desktop.
Save jpotts18/9985611 to your computer and use it in GitHub Desktop.
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