mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Testing nested serializers with models that have str foreign key references.
This commit is contained in:
parent
05c396cfa1
commit
52db4eadc2
|
@ -100,6 +100,9 @@ INSTALLED_APPS = (
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
'rest_framework.authtoken',
|
'rest_framework.authtoken',
|
||||||
'rest_framework.tests',
|
'rest_framework.tests',
|
||||||
|
'rest_framework.tests.accounts',
|
||||||
|
'rest_framework.tests.records',
|
||||||
|
'rest_framework.tests.users',
|
||||||
)
|
)
|
||||||
|
|
||||||
# OAuth is optional and won't work if there is no oauth_provider & oauth2
|
# OAuth is optional and won't work if there is no oauth_provider & oauth2
|
||||||
|
|
0
rest_framework/tests/accounts/__init__.py
Normal file
0
rest_framework/tests/accounts/__init__.py
Normal file
8
rest_framework/tests/accounts/models.py
Normal file
8
rest_framework/tests/accounts/models.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
from rest_framework.tests.users.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class Account(models.Model):
|
||||||
|
owner = models.ForeignKey(User, related_name='accounts_owned')
|
||||||
|
admins = models.ManyToManyField(User, blank=True, null=True, related_name='accounts_administered')
|
11
rest_framework/tests/accounts/serializers.py
Normal file
11
rest_framework/tests/accounts/serializers.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from rest_framework.tests.accounts.models import Account
|
||||||
|
from rest_framework.tests.users.serializers import UserSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class AccountSerializer(serializers.ModelSerializer):
|
||||||
|
admins = UserSerializer(many=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Account
|
0
rest_framework/tests/records/__init__.py
Normal file
0
rest_framework/tests/records/__init__.py
Normal file
6
rest_framework/tests/records/models.py
Normal file
6
rest_framework/tests/records/models.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class Record(models.Model):
|
||||||
|
account = models.ForeignKey('accounts.Account', blank=True, null=True)
|
||||||
|
owner = models.ForeignKey('users.User', blank=True, null=True)
|
|
@ -346,3 +346,8 @@ class NestedModelSerializerUpdateTests(TestCase):
|
||||||
result.save()
|
result.save()
|
||||||
self.assertEqual(result.id, john.id)
|
self.assertEqual(result.id, john.id)
|
||||||
|
|
||||||
|
|
||||||
|
class ImportingModelSerializerWithStrForeignKeys(TestCase):
|
||||||
|
def test_import_model_serializer(self):
|
||||||
|
from rest_framework.tests.accounts.serializers import AccountSerializer
|
||||||
|
self.assertIsInstance(AccountSerializer(), serializers.ModelSerializer)
|
||||||
|
|
0
rest_framework/tests/users/__init__.py
Normal file
0
rest_framework/tests/users/__init__.py
Normal file
6
rest_framework/tests/users/models.py
Normal file
6
rest_framework/tests/users/models.py
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class User(models.Model):
|
||||||
|
account = models.ForeignKey('accounts.Account', blank=True, null=True, related_name='users')
|
||||||
|
active_record = models.ForeignKey('records.Record', blank=True, null=True)
|
8
rest_framework/tests/users/serializers.py
Normal file
8
rest_framework/tests/users/serializers.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
from rest_framework.tests.users.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = User
|
Loading…
Reference in New Issue
Block a user