Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Docs.

  • Loading branch information...
commit 67298f452fe5c31df11b4b6e8a931cce2dec22c1 1 parent 179ab3f
Issac Kelly issackelly authored
Showing with 71 additions and 0 deletions.
  1. +70 −0 docs/content_types.rst
  2. +1 −0  docs/index.rst
70 docs/content_types.rst
View
@@ -0,0 +1,70 @@
+.. _ref-content_types:
+
+==================================
+ContentTypes and GenricForeignKeys
+==================================
+
+`Content Types`_ and GenericForeignKeys are for relationships where the model on
+one end is not defined by the model's schema.
+
+.. _Content Types: https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/
+
+If you're using GenericForeignKeys in django, you can use a
+GenericForeignKeyField in Tastypie.
+
+Usage
+=====
+
+Here's an example model with a GenericForeignKey taken from the Django docs::
+
+ from django.db import models
+ from django.contrib.contenttypes.models import ContentType
+ from django.contrib.contenttypes import generic
+
+ class TaggedItem(models.Model):
+ tag = models.SlugField()
+ content_type = models.ForeignKey(ContentType)
+ object_id = models.PositiveIntegerField()
+ content_object = generic.GenericForeignKey('content_type', 'object_id')
+
+ def __unicode__(self):
+ return self.tag
+
+A simple ModelResource for this model might look like this::
+
+ from tastypie.contrib.contenttypes.fields import GenericForeignKeyField
+ from tastypie.resources import ModelResource
+
+ from .models import Note, Quote, TaggedItem
+
+
+ class QuoteResource(ModelResource):
+
+ class Meta:
+ resource_name = 'quotes'
+ queryset = Quote.objects.all()
+
+
+ class NoteResource(ModelResource):
+
+ class Meta:
+ resource_name = 'notes'
+ queryset = Note.objects.all()
+
+
+ class TaggedItemResource(ModelResource):
+ content_object = GenericForeignKeyField({
+ Note: NoteResource,
+ Quote: QuoteResource
+ }, 'content_object')
+
+ class Meta:
+ resource_name = 'tagged_items'
+ queryset = TaggedItem.objects.all()
+
+Like ToOneField, you must add your GenericForeignKey attribute to your
+ModelResource definition. It will not be added automatically like most other
+field or attribute types. When you define it, you must also define the other
+models and match them to their resources in a dictionary, and pass that as the
+first argument, the second argument is the name of the attribute on the model
+that holds the GenericForeignKey.
1  docs/index.rst
View
@@ -26,6 +26,7 @@ interfaces.
throttling
paginator
geodjango
+ content_types
cookbook
debugging
Please sign in to comment.
Something went wrong with that request. Please try again.