Created
February 14, 2015 21:13
-
-
Save sonthonaxrk/1da9a189d4fdfa0766d6 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
{ | |
"url": "https://api.localhost:8000/users/3/", | |
"first_name": "Steven ", | |
"last_name": "Konig", | |
"email": "[email protected]", | |
"profile-image": "https://fbcdn-profile-a.akamaihd.net/profile_picture.jpg", | |
"age": 36, | |
"profiles": { | |
"customer_profile_url": "https://api.localhost:8000/customers/3/", | |
"vendor_profile_url": "https://api.localhost:8000/vendors/3/" | |
} | |
}, |
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
class UserSerializer(serializers.HyperlinkedModelSerializer): | |
def __init__(self, *args, **kwargs): | |
super(UserSerializer, self).__init__(*args, **kwargs) | |
trim_user_data = self.context['request'].QUERY_PARAMS.get('trim_user_data') | |
if trim_user_data in ['true', '1', 'True']: | |
allowed = set(['url', 'id', 'profiles']) | |
existing = set(self.fields.keys()) | |
for field_name in existing - allowed: | |
self.fields.pop(field_name) | |
profiles = serializers.SerializerMethodField() | |
def get_profiles(self, instance): | |
profile_dict = {} | |
if hasattr(instance, 'customerprofile'): | |
e = CustomerProfileSerializer(instance.customerprofile, context=self.context) | |
pdb.set_trace() | |
profile_dict['customer_profile_url'] = e['url'].value | |
if hasattr(instance, 'vendorprofile'): | |
e = VendorProfileSerializer(instance.vendorprofile, context=self.context ) | |
profile_dict['vendor_profile_url'] = e['url'].value | |
return profile_dict | |
class Meta: | |
model = User | |
fields = ('url', | |
'id', | |
'first_name', | |
'last_name', | |
'get_full_name', | |
'email', | |
'profiles', | |
) | |
extra_kwargs={ | |
'url': | |
{'view_name': 'user-detail'} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment