Skip to content
Browse files

added tests and documentation for representing a reverse GenericRelation

  • Loading branch information...
1 parent 6af29d8 commit 0b84b7ef3eb7fb69e2ef135da4bae41aacd1ab38 @dhatch committed Jul 6, 2011
View
4 tastypie/fields.py
@@ -697,6 +697,10 @@ class ToManyField(RelatedField):
Note that the ``hydrate`` portions of this field are quite different than
any other field. ``hydrate_m2m`` actually handles the data and relations.
This is due to the way Django implements M2M relationships.
+
+ Can be used to represent a ``GenericRelation``
+ (reverse of ``GenericForeignKey``). Simply set ``to`` to the resource
+ which represents the other end of the relation.
"""
is_m2m = True
help_text = 'Many related resources. Can be either a list of URIs or list of individually nested resource data.'
View
2 tests/related_resource/api/resources.py
@@ -24,7 +24,7 @@ class Meta:
class CategoryResource(ModelResource):
parent = fields.ToOneField('self', 'parent', null=True)
-
+ tags = fields.ToManyField('related_resource.api.resources.GenericTagResource', attribute="tags", null=True, blank=True)
class Meta:
resource_name = 'category'
queryset = Category.objects.all()
View
2 tests/related_resource/models.py
@@ -5,7 +5,7 @@
class Category(models.Model):
parent = models.ForeignKey('self', null=True)
name = models.CharField(max_length=32)
-
+ tags = generic.GenericRelation('related_resource.GenericTag')
def __unicode__(self):
return u"%s (%s)" % (self.name, self.parent)
View
41 tests/related_resource/tests.py
@@ -391,4 +391,43 @@ def test_put(self):
ContentType.objects.get_for_model(Taggable))))
resp = resource.put_detail(request, pk=data['id'])
self.assertEqual(resp.status_code, 204)
- self.assertEqual(GenericTag.objects.get(pk=data['id']).content_type, ContentType.objects.get_for_model(Taggable))
+ self.assertEqual(GenericTag.objects.get(pk=data['id']).content_type, ContentType.objects.get_for_model(Taggable))
+
+ def test_reverse(self):
+ tags = self.category_1.tags.all()
+
+ request = MockRequest()
+ request.GET = {'format': 'json'}
+ request.method = 'GET'
+
+ resource = api.canonical_resource_for('generictag')
+ resource.fields['content_object'].full = False
+ # should get us self.tag_1
+ resp = resource.wrap_view('dispatch_detail')(request, pk=self.tag_1.pk)
+ self.assertEqual(resp.status_code, 200)
+ data = json.loads(resp.content)
+ # test that the uri for content_object pointed to self.category_1
+ self.assertEqual(data['content_object'],
+ CategoryResource().get_resource_uri(self.category_1))
+
+ resp = self.client.get(data['content_object'], data={"format": "json"})
+ self.assertEqual(resp.status_code, 200)
+ data = json.loads(resp.content)
+ tags_urls = list()
+ for tag in tags:
+ tags_urls.append(GenericTagResource().get_resource_uri(tag))
+ self.assertEqual(data['tags'], tags_urls)
+
+ # add some more tags to this category
+ GenericTag.objects.create(name="Object Orientated", content_object=self.category_1)
+ GenericTag.objects.create(name="Interpreted", content_object=self.category_1)
+
+ tags = self.category_1.tags.all()
+ tags_urls = list()
+ for tag in tags:
+ tags_urls.append(GenericTagResource().get_resource_uri(tag))
+
+ resp = self.client.get(data['resource_uri'], data={"format": "json"})
+ self.assertEqual(resp.status_code, 200)
+ data = json.loads(resp.content)
+ self.assertEqual(data['tags'], tags_urls)

0 comments on commit 0b84b7e

Please sign in to comment.
Something went wrong with that request. Please try again.