Fix for field to make it easier to access field relationships

This commit is contained in:
Tom Christie 2012-10-04 22:07:24 +01:00
parent cc0d2601b8
commit 693892ed01
3 changed files with 62 additions and 1 deletions

View File

@ -83,6 +83,10 @@ class Field(object):
if is_protected_type(value):
return value
all_callable = getattr(value, 'all', None)
if is_simple_callable(all_callable):
return [self.to_native(item) for item in value.all()]
elif hasattr(value, '__iter__') and not isinstance(value, (dict, basestring)):
return [self.to_native(item) for item in value]
return smart_unicode(value)
@ -197,7 +201,7 @@ class ModelField(WritableField):
value = self.model_field._get_val_from_obj(obj)
if is_protected_type(value):
return value
return self.model_field.value_to_string(self.obj)
return self.model_field.value_to_string(obj)
def attributes(self):
return {

View File

@ -0,0 +1,33 @@
from django.test import TestCase
from rest_framework import serializers
from rest_framework.tests.models import *
class TestGenericRelations(TestCase):
def setUp(self):
bookmark = Bookmark(url='https://www.djangoproject.com/')
bookmark.save()
django = Tag(tag_name='django')
django.save()
python = Tag(tag_name='python')
python.save()
t1 = TaggedItem(content_object=bookmark, tag=django)
t1.save()
t2 = TaggedItem(content_object=bookmark, tag=python)
t2.save()
self.bookmark = bookmark
def test_reverse_generic_relation(self):
class BookmarkSerializer(serializers.ModelSerializer):
tags = serializers.Field(source='tags')
class Meta:
model = Bookmark
exclude = ('id',)
serializer = BookmarkSerializer(instance=self.bookmark)
expected = {
'tags': [u'django', u'python'],
'url': u'https://www.djangoproject.com/'
}
self.assertEquals(serializer.data, expected)

View File

@ -1,4 +1,7 @@
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericForeignKey, GenericRelation
# from django.contrib.auth.models import Group
@ -59,3 +62,24 @@ class CallableDefaultValueModel(RESTFrameworkModel):
class ManyToManyModel(RESTFrameworkModel):
rel = models.ManyToManyField(Anchor)
# Models to test generic relations
class Tag(RESTFrameworkModel):
tag_name = models.SlugField()
class TaggedItem(RESTFrameworkModel):
tag = models.ForeignKey(Tag, related_name='items')
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
def __unicode__(self):
return self.tag.tag_name
class Bookmark(RESTFrameworkModel):
url = models.URLField()
tags = GenericRelation(TaggedItem)