mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-30 21:44:04 +03:00
Add a real world example
This commit is contained in:
parent
705e277896
commit
c1ccc37097
|
@ -13,6 +13,7 @@ import tempfile
|
||||||
|
|
||||||
import django
|
import django
|
||||||
import pytest
|
import pytest
|
||||||
|
from django.contrib.auth.models import User
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.core.serializers.json import DjangoJSONEncoder
|
from django.core.serializers.json import DjangoJSONEncoder
|
||||||
from django.core.validators import (
|
from django.core.validators import (
|
||||||
|
@ -736,25 +737,40 @@ class TestRelationalFieldMappings(TestCase):
|
||||||
self.assertEqual(repr(TestSerializer()), expected)
|
self.assertEqual(repr(TestSerializer()), expected)
|
||||||
|
|
||||||
def test_source_with_attributes(self):
|
def test_source_with_attributes(self):
|
||||||
class TestSerializer(serializers.ModelSerializer):
|
class UserProfile(models.Model):
|
||||||
|
age = models.IntegerField()
|
||||||
|
birthdate = models.DateField()
|
||||||
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
|
|
||||||
|
class UserProfileSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = RelationalModel
|
model = UserProfile
|
||||||
fields = ('foreign_key_name', 'one_to_one_name')
|
fields = ('username', 'email', 'first_name', 'last_name', 'age', 'birthdate')
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
'foreign_key_name': {
|
'username': {
|
||||||
'source': 'foreign_key.name',
|
'source': 'user.username',
|
||||||
},
|
},
|
||||||
'one_to_one_name': {
|
'email': {
|
||||||
'source': 'one_to_one.name',
|
'source': 'user.email',
|
||||||
|
},
|
||||||
|
'first_name': {
|
||||||
|
'source': 'user.first_name',
|
||||||
|
},
|
||||||
|
'last_name': {
|
||||||
|
'source': 'user.last_name',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
expected = dedent("""
|
expected = dedent("""
|
||||||
TestSerializer():
|
UserProfileSerializer():
|
||||||
foreign_key_name = CharField(max_length=100, source='foreign_key.name')
|
username = CharField(help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, source='user.username', validators=[<django.contrib.auth.validators.UnicodeUsernameValidator object>, <UniqueValidator(queryset=User.objects.all())>])
|
||||||
one_to_one_name = CharField(max_length=100, source='one_to_one.name')
|
email = EmailField(allow_blank=True, label='Email address', max_length=254, required=False, source='user.email')
|
||||||
|
first_name = CharField(allow_blank=True, max_length=150, required=False, source='user.first_name')
|
||||||
|
last_name = CharField(allow_blank=True, max_length=150, required=False, source='user.last_name')
|
||||||
|
age = IntegerField()
|
||||||
|
birthdate = DateField()
|
||||||
""")
|
""")
|
||||||
self.assertEqual(repr(TestSerializer()), expected)
|
self.assertEqual(repr(UserProfileSerializer()), expected)
|
||||||
|
|
||||||
|
|
||||||
class DisplayValueTargetModel(models.Model):
|
class DisplayValueTargetModel(models.Model):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user