django-contentrelations 0.3.1 documentation

Creating a Resource

Establishing the interface

Before you do anything, plan out which attributes you want to unify. For out example, we’ll say we want title, description, key_image and url.

With that in mind, we’ll create our Resource models. Let’s start with the Image model:

from contentrelations import BaseResource, resource_list
from imageapp.models import Image

class ImageResource(BaseResource):
    def get_title(self):
        return self.instance.name

    def get_description(self):
        return self.instance.caption

    def get_key_image(self):
        return self.instance.image


resource_list.register(Image, ImageResource)

Our ImageResource class subclasses BaseResource, which provides a bunch of helpful features. We created three methods get_title(), get_description(), and get_key_image(), and in each of these methods, we simply returned the equivalent attribute for that model using the instance attribute.

Aren’t we missing something? What about the URL? Well, that leads us to discuss some features of BaseResource.

Special Attributes of BaseResource

For the most part BaseResource resolves requests for attributes in a specific and predictable way (see Accessing resource attributes). There are two special attributes that it handles differently: resource_type and url.

Resource Name

By default BaseResource returns the model’s verbose name. This attribute becomes a standard way to refer to the type of resource. You can override the default value by defining a get_resource_name method in your sub-class.

URL

By default BaseResource calls the instance’s get_absolute_url method and returns the result. In some cases, you may want to return a different value, as we will when we define the Resource class for DownloadableFile.

Customizing the special attributes

Let’s define our interface for our DownloadableFile model.

from contentrelations import BaseResource, resource_list
from downloads.models import DownloadableFile

class DownloadableFileResource(BaseResource):
    def get_title(self):
        return self.instance.file_name

    def get_description(self):
        return self.instance.notes

    def get_key_image(self):
        return self.instance.preview_image

    def get_url(self):
        return self.instance.downloadablefile.url


resource_list.register(DownloadableFile, DownloadableFileResource)

You’ll notice that we defined four get_ methods, and the get_url method returns the URL for the file instead of the model.

Finally, let’s define the interface for our Article model.

from contentrelations import BaseResource, resource_list
from articleapp.models import Article

class ArticleResource(BaseResource):
    def get_title(self):
        return self.instance.headline

    def get_description(self):
        return self.instance.summary

    def get_key_image(self):
        return self.instance.primary_image

    def get_resource_name(self):
        return u'%s Article' % self.instance.primary_category

resource_list.register(Article, ArticleResource)

In this case, we defined a get_resource_name. The version for our Article returns a different name depending on each instance’s primary_category.

Where do I put these definitions?

Good question! Django Supply Closet attempts to import the resources module of every installed app package. So define these in a resource.py file.