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

This commit is contained in:
Dustin Farris 2014-01-08 16:14:27 -05:00
parent 05c396cfa1
commit 52db4eadc2
10 changed files with 47 additions and 0 deletions

View File

@ -100,6 +100,9 @@ INSTALLED_APPS = (
'rest_framework',
'rest_framework.authtoken',
'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

View 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')

View 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

View File

View 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)

View File

@ -346,3 +346,8 @@ class NestedModelSerializerUpdateTests(TestCase):
result.save()
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)

View File

View 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)

View 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