Created
April 12, 2011 17:27
-
-
Save toastdriven/915978 to your computer and use it in GitHub Desktop.
First example uses a ``OneToOne`` profile, the second uses the traditional Django profile bits.
This file contains 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 tastypie.authentication import ApiKeyAuthentication | |
from tastypie.authorization import DjangoAuthorization | |
from tastypie import fields | |
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS | |
from core.api.authentication import OpenReadApiKeyAuthentication | |
class UserResource(ModelResource): | |
bio = fields.CharField(attribute='profile__bio', null=True) | |
website = fields.CharField(attribute='profile__website', null=True) | |
twitter_username = fields.CharField(attribute='profile__twitter_username', null=True) | |
characters_written = fields.IntegerField(attribute='profile__characters_written', readonly=True) | |
class Meta: | |
queryset = User.objects.filter(is_active=True) | |
resource_name = 'users' | |
include_absolute_url = True | |
# authentication = ApiKeyAuthentication() | |
authentication = OpenReadApiKeyAuthentication() | |
authorization = DjangoAuthorization() | |
excludes = ['email', 'password', 'is_staff', 'is_superuser', 'is_active'] | |
filtering = { | |
'username': ALL, | |
} |
This file contains 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 tastypie.authentication import ApiKeyAuthentication | |
from tastypie.authorization import DjangoAuthorization | |
from tastypie import fields | |
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS | |
from core.api.authentication import OpenReadApiKeyAuthentication | |
class UserResource(ModelResource): | |
bio = fields.CharField(attribute='get_profile__bio', null=True) | |
website = fields.CharField(attribute='get_profile__website', null=True) | |
twitter_username = fields.CharField(attribute='get_profile__twitter_username', null=True) | |
characters_written = fields.IntegerField(attribute='get_profile__characters_written', readonly=True) | |
class Meta: | |
queryset = User.objects.filter(is_active=True) | |
resource_name = 'users' | |
include_absolute_url = True | |
# authentication = ApiKeyAuthentication() | |
authentication = OpenReadApiKeyAuthentication() | |
authorization = DjangoAuthorization() | |
excludes = ['email', 'password', 'is_staff', 'is_superuser', 'is_active'] | |
filtering = { | |
'username': ALL, | |
} | |
def hydrate(self, bundle): | |
updated_bundle = super(UserResource, self).hydrate(bundle) | |
profile = bundle.obj.get_profile() | |
profile.bio = bundle.data.get('bio', '') | |
profile.website = bundle.data.get('website', '') | |
profile.twitter_username = bundle.data.get('twitter_username', '') | |
profile.save() | |
return updated_bundle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment