Package: django-tastypie-extendedmodelresource
TheExtendedModelResourceis an extension for TastyPie’sModelResourcethat adds some interesting features:Supports easily using resources asnestedof another resource, with proper authorization checks for each case.[This feature has already been included in the official TastyPie] Supports using a custom identifier attribute for resources in uris (not only the object’s pk!)RequirementsRequiredLatest django-tastypie from repository (0.9.12-alpha or hopefully greater) and its requirements.OptionalDjango 1.4.1 for the sample project.InstallationClone repository and do:python setup.py installOr just dopip install django-tastypie-extendedmodelresourceto get the latest version frompypi.NestedresourcesHere we explain what we mean bynested resourcesand what a simple use case would be.RationaleImagine you have a simple application which has users, each of which can write any number of entries. Every entry is associated to a user. For examplefrom django.contrib.auth.models import User
from django.db import models


class Entry(models.Model):
    user = models.ForeignKey(User, related_name='entries')
    # ... more fieldsThe ‘standard’ TastyPie models for this would befrom django.contrib.auth.models import User
from tastypie.resources import ModelResource

from myapp.models import Entry


class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()

class EntryResource(ModelResource):
    class Meta:
        queryset = Entry.objects.all()This gives you full CRUD ability over users and entries, with uris such as/api/user/and/api/entry/.Now imagine you want to be able to easily list all the entries authored by a given user, with a uri such as/api/user/<pk>/entries. Additionally, you would like to be able to POST to this uri and create an entry automatically associated to this user. This is why nested resources are for.If a resource such as theEntryResourceis to be accessed as/api/user/<pk>/<something>where<something>is custom-defined (for exampleentries), then we say theEntryResourceis being used asnestedof theUserResource. We also say thatUserResourceis theparentofEntryResource.The standard TastyPie’sModelResourcewould force you to write a function overriding the urls of theUserResourceand adding a method to handle the entry resource (seeNested Resources). UsingExtendedModelResourceit is as easy asfrom django.contrib.auth.models import User
from tastypie import fields

from extendedmodelresource import ExtendedModelResource
from myapp.models import Entry


class UserResource(ExtendedModelResource):
    class Meta:
        queryset = User.objects.all()

    class Nested:
        entries = fields.ToManyField('api.resources.EntryResource', 'entries')


class EntryResource(ExtendedModelResource):
    user = fields.ForeignKey(UserResource, 'user')

    class Meta:
        queryset = Entry.objects.all()And that’s it!How authorization is handledIf a resource does not have a nested resource, the authorization is handled the same way as in the standard TastyPie. You define anAuthorizationclass and associate it to the resource. This class may implement theis_authorizedandapply_limitsmethods.For anExtendedModelResourcewith nesteds, all the authorization when using the nested as such is handled from the authorization classof the parent resource. For each resource used as nested, theAuthorizationclass of the parent can implement two methods:is_authorized_nested_<attribute>apply_limits_nested_<attribute>where<attribute>is the name of the attribute parameter in theApiFieldthat declares the resource as nested. These functions work identically to the original ones, except that they also receive aparent_objectparameter which will contain the parent object.For our users and entries example, anAuthorizationcan be something like:from tastypie.authorization import Authorization


class UserResourceAuthorization(Authorization):
    """
    Our Authorization class for UserResource and its nested.
    """

    def is_authorized(self, request, object=None):
        # Only 'newton' is authorized to view the users
        if 'newton' in request.user.username:
          return True

        return False

    def apply_limits(self, request, object_list):
        return object_list.all()

    def is_authorized_nested_entries(self, request,
                                     parent_object, object=None):
        # Is request.user authorized to access the EntryResource as
        # nested?
        return True

    def apply_limits_nested_entries(self, request, parent_object,
                                   object_list):
        # Advanced filtering.
        # Note that object_list already only contains the objects that
        # are associated to parent_object.
        return object_list.all()CaveatsExtendedModelResourceonly supports one level nesting.Resources used as nested can also be registered in anApiinstance, but need not to. That is, there can be resources usedonlyas nested and not exposed otherwise in the urls.Changing object’s identifier attribute in urlsUsing the latest TastyPie you can define adetail_uri_nameattribute
in theMetaclass, to use a different attribute than the object’spkclass UserResource(ExtendedModelResource):
    class Meta:
        queryset = User.objects.all()
        detail_uri_name = 'username'WithExtendedModelResourceyou can change the regular expression used for your identifier attribute in the urls, you can override the methodget_url_id_attribute_regexand return it, like the following exampledef get_detail_uri_name_regex(self):
    return r'[aA-zZ][\w-]*'More informationDate:08-20-2012Version:0.2Authors:Alan Descoins - Tryolabs <alan@tryolabs.com>Martín Santos - Tryolabs <santos@tryolabs.com>Website:https://github.com/tryolabs/django-tastypie-extendedmodelresource