Commit 52db4ead by Dustin Farris

Testing nested serializers with models that have str foreign key references.

parent 05c396cf
...@@ -100,6 +100,9 @@ INSTALLED_APPS = ( ...@@ -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
......
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')
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
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): ...@@ -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)
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)
from rest_framework import serializers
from rest_framework.tests.users.models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment