mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Merge pull request #278 from tomchristie/related-field-fixes
Related field fixes
This commit is contained in:
commit
ad2149767b
43
docs/api-guide/fields.md
Normal file
43
docs/api-guide/fields.md
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<a class="github" href="fields.py"></a>
|
||||||
|
|
||||||
|
# Serializer fields
|
||||||
|
|
||||||
|
> Flat is better than nested.
|
||||||
|
>
|
||||||
|
> — [The Zen of Python][cite]
|
||||||
|
|
||||||
|
# Generic Fields
|
||||||
|
|
||||||
|
## Field
|
||||||
|
|
||||||
|
## ModelField
|
||||||
|
|
||||||
|
# Typed Fields
|
||||||
|
|
||||||
|
## BooleanField
|
||||||
|
|
||||||
|
## CharField
|
||||||
|
|
||||||
|
## EmailField
|
||||||
|
|
||||||
|
## DateField
|
||||||
|
|
||||||
|
## DateTimeField
|
||||||
|
|
||||||
|
## IntegerField
|
||||||
|
|
||||||
|
## FloatField
|
||||||
|
|
||||||
|
# Relational Fields
|
||||||
|
|
||||||
|
Relational fields are used to represent model relationships.
|
||||||
|
|
||||||
|
## PrimaryKeyRelatedField
|
||||||
|
|
||||||
|
## ManyPrimaryKeyRelatedField
|
||||||
|
|
||||||
|
## HyperlinkedRelatedField
|
||||||
|
|
||||||
|
## ManyHyperlinkedRelatedField
|
||||||
|
|
||||||
|
[cite]: http://www.python.org/dev/peps/pep-0020/
|
|
@ -28,25 +28,34 @@ from django.db import models
|
||||||
# 'pk': self.id
|
# 'pk': self.id
|
||||||
# })
|
# })
|
||||||
|
|
||||||
class Anchor(models.Model):
|
def foobar():
|
||||||
|
return 'foobar'
|
||||||
|
|
||||||
|
|
||||||
|
class RESTFrameworkModel(models.Model):
|
||||||
"""
|
"""
|
||||||
A simple model to use as the target of relationships for other test models.
|
Base for test models that sets app_label, so they play nicely.
|
||||||
"""
|
"""
|
||||||
|
class Meta:
|
||||||
|
app_label = 'rest_framework'
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
|
||||||
|
class Anchor(RESTFrameworkModel):
|
||||||
text = models.CharField(max_length=100, default='anchor')
|
text = models.CharField(max_length=100, default='anchor')
|
||||||
|
|
||||||
class Meta:
|
|
||||||
app_label = 'rest_framework'
|
|
||||||
|
|
||||||
|
class BasicModel(RESTFrameworkModel):
|
||||||
class BasicModel(models.Model):
|
|
||||||
text = models.CharField(max_length=100)
|
text = models.CharField(max_length=100)
|
||||||
|
|
||||||
class Meta:
|
|
||||||
app_label = 'rest_framework'
|
class DefaultValueModel(RESTFrameworkModel):
|
||||||
|
text = models.CharField(default='foobar', max_length=100)
|
||||||
|
|
||||||
|
|
||||||
class ManyToManyModel(models.Model):
|
class CallableDefaultValueModel(RESTFrameworkModel):
|
||||||
|
text = models.CharField(default=foobar, max_length=100)
|
||||||
|
|
||||||
|
|
||||||
|
class ManyToManyModel(RESTFrameworkModel):
|
||||||
rel = models.ManyToManyField(Anchor)
|
rel = models.ManyToManyField(Anchor)
|
||||||
|
|
||||||
class Meta:
|
|
||||||
app_label = 'rest_framework'
|
|
||||||
|
|
|
@ -202,9 +202,58 @@ class ManyToManyTests(TestCase):
|
||||||
self.assertEquals(instance.pk, 2)
|
self.assertEquals(instance.pk, 2)
|
||||||
self.assertEquals(list(instance.rel.all()), [])
|
self.assertEquals(list(instance.rel.all()), [])
|
||||||
|
|
||||||
# def test_deserialization_for_update(self):
|
|
||||||
# serializer = self.serializer_class(self.data, instance=self.instance)
|
class DefaultValueTests(TestCase):
|
||||||
# expected = self.instance
|
def setUp(self):
|
||||||
# self.assertEquals(serializer.is_valid(), True)
|
class DefaultValueSerializer(serializers.ModelSerializer):
|
||||||
# self.assertEquals(serializer.object, expected)
|
class Meta:
|
||||||
# self.assertTrue(serializer.object is expected)
|
model = DefaultValueModel
|
||||||
|
|
||||||
|
self.serializer_class = DefaultValueSerializer
|
||||||
|
self.objects = DefaultValueModel.objects
|
||||||
|
|
||||||
|
def test_create_using_default(self):
|
||||||
|
data = {}
|
||||||
|
serializer = self.serializer_class(data)
|
||||||
|
self.assertEquals(serializer.is_valid(), True)
|
||||||
|
instance = serializer.save()
|
||||||
|
self.assertEquals(len(self.objects.all()), 1)
|
||||||
|
self.assertEquals(instance.pk, 1)
|
||||||
|
self.assertEquals(instance.text, 'foobar')
|
||||||
|
|
||||||
|
def test_create_overriding_default(self):
|
||||||
|
data = {'text': 'overridden'}
|
||||||
|
serializer = self.serializer_class(data)
|
||||||
|
self.assertEquals(serializer.is_valid(), True)
|
||||||
|
instance = serializer.save()
|
||||||
|
self.assertEquals(len(self.objects.all()), 1)
|
||||||
|
self.assertEquals(instance.pk, 1)
|
||||||
|
self.assertEquals(instance.text, 'overridden')
|
||||||
|
|
||||||
|
|
||||||
|
class CallableDefaultValueTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
class CallableDefaultValueSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = CallableDefaultValueModel
|
||||||
|
|
||||||
|
self.serializer_class = CallableDefaultValueSerializer
|
||||||
|
self.objects = CallableDefaultValueModel.objects
|
||||||
|
|
||||||
|
def test_create_using_default(self):
|
||||||
|
data = {}
|
||||||
|
serializer = self.serializer_class(data)
|
||||||
|
self.assertEquals(serializer.is_valid(), True)
|
||||||
|
instance = serializer.save()
|
||||||
|
self.assertEquals(len(self.objects.all()), 1)
|
||||||
|
self.assertEquals(instance.pk, 1)
|
||||||
|
self.assertEquals(instance.text, 'foobar')
|
||||||
|
|
||||||
|
def test_create_overriding_default(self):
|
||||||
|
data = {'text': 'overridden'}
|
||||||
|
serializer = self.serializer_class(data)
|
||||||
|
self.assertEquals(serializer.is_valid(), True)
|
||||||
|
instance = serializer.save()
|
||||||
|
self.assertEquals(len(self.objects.all()), 1)
|
||||||
|
self.assertEquals(instance.pk, 1)
|
||||||
|
self.assertEquals(instance.text, 'overridden')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user