mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-23 22:49:50 +03:00
add tests and doc changes for 'pk' #350,
modify get_fields to allow excluding using 'pk' shorctut as well **Ammend**: remove un-intended change in setup.py
This commit is contained in:
parent
bf4e6234be
commit
2b264cc3e8
|
@ -236,6 +236,8 @@ For example:
|
||||||
model = Account
|
model = Account
|
||||||
exclude = ('id',)
|
exclude = ('id',)
|
||||||
|
|
||||||
|
**Note**: All fields names in either `fields` or `exclude` must already be defined in the model or set explicitly, with the exception of `pk` which is a shortcut field to the actual primary key field of the model (by default Django sets this to `id` but can be overidden).
|
||||||
|
|
||||||
## Specifiying nested serialization
|
## Specifiying nested serialization
|
||||||
|
|
||||||
The default `ModelSerializer` uses primary keys for relationships, but you can also easily generate nested representations using the `depth` option:
|
The default `ModelSerializer` uses primary keys for relationships, but you can also easily generate nested representations using the `depth` option:
|
||||||
|
|
|
@ -201,7 +201,7 @@ Open the file `snippets/serializers.py` again, and edit the `SnippetSerializer`
|
||||||
class SnippetSerializer(serializers.ModelSerializer):
|
class SnippetSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Snippet
|
model = Snippet
|
||||||
fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
|
fields = ('pk', 'title', 'code', 'linenos', 'language', 'style')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,7 @@ Now that we've got some users to work with, we'd better add representations of t
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ('id', 'username', 'snippets')
|
fields = ('pk', 'username', 'snippets')
|
||||||
|
|
||||||
Because `'snippets'` is a *reverse* relationship on the User model, it will not be included by default when using the `ModelSerializer` class, so we've needed to add an explicit field for it.
|
Because `'snippets'` is a *reverse* relationship on the User model, it will not be included by default when using the `ModelSerializer` class, so we've needed to add an explicit field for it.
|
||||||
|
|
||||||
|
|
|
@ -130,6 +130,8 @@ class BaseSerializer(Field):
|
||||||
# Set up the field
|
# Set up the field
|
||||||
field.initialize(parent=self, field_name=key)
|
field.initialize(parent=self, field_name=key)
|
||||||
|
|
||||||
|
pk_field = None
|
||||||
|
|
||||||
# Add in the default fields
|
# Add in the default fields
|
||||||
fields = self.default_fields(nested)
|
fields = self.default_fields(nested)
|
||||||
for key, val in fields.items():
|
for key, val in fields.items():
|
||||||
|
@ -147,7 +149,7 @@ class BaseSerializer(Field):
|
||||||
new = SortedDict()
|
new = SortedDict()
|
||||||
for key in self.opts.fields:
|
for key in self.opts.fields:
|
||||||
if key == 'pk':
|
if key == 'pk':
|
||||||
# User requested the 'pk', use the primary key found
|
# 'pk' shortcut: use the primary key found
|
||||||
new[key] = ret[pk_field]
|
new[key] = ret[pk_field]
|
||||||
continue
|
continue
|
||||||
new[key] = ret[key]
|
new[key] = ret[key]
|
||||||
|
@ -156,6 +158,9 @@ class BaseSerializer(Field):
|
||||||
# Remove anything in 'exclude'
|
# Remove anything in 'exclude'
|
||||||
if self.opts.exclude:
|
if self.opts.exclude:
|
||||||
for key in self.opts.exclude:
|
for key in self.opts.exclude:
|
||||||
|
if key == 'pk':
|
||||||
|
# 'pk' shortcut: remove the primary key found
|
||||||
|
ret.pop(pk_field)
|
||||||
ret.pop(key, None)
|
ret.pop(key, None)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
|
@ -50,7 +50,7 @@ class PersonSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Person
|
model = Person
|
||||||
fields = ('name', 'age', 'info')
|
fields = ('pk', 'name', 'age', 'info')
|
||||||
|
|
||||||
|
|
||||||
class BasicTests(TestCase):
|
class BasicTests(TestCase):
|
||||||
|
@ -112,7 +112,7 @@ class BasicTests(TestCase):
|
||||||
"""
|
"""
|
||||||
serializer = PersonSerializer(self.person)
|
serializer = PersonSerializer(self.person)
|
||||||
self.assertEquals(set(serializer.data.keys()),
|
self.assertEquals(set(serializer.data.keys()),
|
||||||
set(['name', 'age', 'info']))
|
set(['pk', 'name', 'age', 'info']))
|
||||||
|
|
||||||
def test_field_with_dictionary(self):
|
def test_field_with_dictionary(self):
|
||||||
""" Make sure that dictionaries from fields are left intact
|
""" Make sure that dictionaries from fields are left intact
|
||||||
|
@ -121,6 +121,14 @@ class BasicTests(TestCase):
|
||||||
expected = self.person_data
|
expected = self.person_data
|
||||||
self.assertEquals(serializer.data['info'], expected)
|
self.assertEquals(serializer.data['info'], expected)
|
||||||
|
|
||||||
|
def test_pk_field_exists(self):
|
||||||
|
"""
|
||||||
|
Verify the `pk` shortcut for primary key (by default: `id`)
|
||||||
|
"""
|
||||||
|
|
||||||
|
serializer = PersonSerializer(self.person)
|
||||||
|
self.assertEquals(serializer.data['pk'], self.person.id)
|
||||||
|
|
||||||
|
|
||||||
class ValidationTests(TestCase):
|
class ValidationTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user