Skip to content

Commit

Permalink
added tests and documentation for representing a reverse GenericRelation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhatch committed Jul 6, 2011
1 parent 6af29d8 commit 0b84b7e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions tastypie/fields.py
Expand Up @@ -697,6 +697,10 @@ class ToManyField(RelatedField):
Note that the ``hydrate`` portions of this field are quite different than Note that the ``hydrate`` portions of this field are quite different than
any other field. ``hydrate_m2m`` actually handles the data and relations. any other field. ``hydrate_m2m`` actually handles the data and relations.
This is due to the way Django implements M2M relationships. 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 is_m2m = True
help_text = 'Many related resources. Can be either a list of URIs or list of individually nested resource data.' help_text = 'Many related resources. Can be either a list of URIs or list of individually nested resource data.'
Expand Down
2 changes: 1 addition & 1 deletion tests/related_resource/api/resources.py
Expand Up @@ -24,7 +24,7 @@ class Meta:


class CategoryResource(ModelResource): class CategoryResource(ModelResource):
parent = fields.ToOneField('self', 'parent', null=True) parent = fields.ToOneField('self', 'parent', null=True)

tags = fields.ToManyField('related_resource.api.resources.GenericTagResource', attribute="tags", null=True, blank=True)
class Meta: class Meta:
resource_name = 'category' resource_name = 'category'
queryset = Category.objects.all() queryset = Category.objects.all()
Expand Down
2 changes: 1 addition & 1 deletion tests/related_resource/models.py
Expand Up @@ -5,7 +5,7 @@
class Category(models.Model): class Category(models.Model):
parent = models.ForeignKey('self', null=True) parent = models.ForeignKey('self', null=True)
name = models.CharField(max_length=32) name = models.CharField(max_length=32)

tags = generic.GenericRelation('related_resource.GenericTag')
def __unicode__(self): def __unicode__(self):
return u"%s (%s)" % (self.name, self.parent) return u"%s (%s)" % (self.name, self.parent)


Expand Down
41 changes: 40 additions & 1 deletion tests/related_resource/tests.py
Expand Up @@ -391,4 +391,43 @@ def test_put(self):
ContentType.objects.get_for_model(Taggable)))) ContentType.objects.get_for_model(Taggable))))
resp = resource.put_detail(request, pk=data['id']) resp = resource.put_detail(request, pk=data['id'])
self.assertEqual(resp.status_code, 204) 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.