Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3104433

Browse files
Added basic test for model resource_name property
1 parent db88326 commit 3104433

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

‎example/models.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ def __str__(self):
3434
return self.name
3535

3636

37+
@python_2_unicode_compatible
38+
class RenamedAuthor(Author):
39+
class Meta:
40+
proxy = True
41+
42+
class JSONAPIMeta:
43+
resource_name = "super-author"
44+
45+
def __str__(self):
46+
return self.name
47+
48+
3749
@python_2_unicode_compatible
3850
class Entry(BaseModel):
3951
blog = models.ForeignKey(Blog)

‎example/serializers.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from rest_framework_json_api import serializers, relations
2-
from example.models import Blog, Entry, Author, Comment
2+
from example.models import Blog, Entry, Author, Comment, RenamedAuthor
33

44

55
class BlogSerializer(serializers.ModelSerializer):
@@ -45,6 +45,13 @@ class Meta:
4545
fields = ('name', 'email',)
4646

4747

48+
class RenamedAuthorSerializer(serializers.ModelSerializer):
49+
50+
class Meta:
51+
model = RenamedAuthor
52+
fields = ('name', 'email',)
53+
54+
4855
class CommentSerializer(serializers.ModelSerializer):
4956

5057
class Meta:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
from django.core.urlresolvers import reverse
3+
4+
from example.tests.utils import load_json
5+
6+
pytestmark = pytest.mark.django_db
7+
8+
9+
def test_model_resource_name_on_list(single_entry, client):
10+
response = client.get(reverse("renamed-authors-list"))
11+
data = load_json(response.content)['data']
12+
# name should be super-author instead of model name RenamedAuthor
13+
assert [x.get('type') for x in data] == ['super-author'], 'List included types are incorrect'

‎example/urls_test.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
from rest_framework import routers
33

44
from example.views import BlogViewSet, EntryViewSet, AuthorViewSet, EntryRelationshipView, BlogRelationshipView, \
5-
CommentRelationshipView, AuthorRelationshipView
5+
CommentRelationshipView, AuthorRelationshipView, RenamedAuthorViewSet
66
from .api.resources.identity import Identity, GenericIdentity
77

88
router = routers.DefaultRouter(trailing_slash=False)
99

1010
router.register(r'blogs', BlogViewSet)
1111
router.register(r'entries', EntryViewSet)
1212
router.register(r'authors', AuthorViewSet)
13+
router.register(r'renamed-authors', RenamedAuthorViewSet, base_name='renamed-authors')
1314

1415
# for the old tests
1516
router.register(r'identities', Identity)

‎example/views.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from rest_framework import viewsets
22
from rest_framework_json_api.views import RelationshipView
3-
from example.models import Blog, Entry, Author, Comment
3+
from example.models import Blog, Entry, Author, Comment, RenamedAuthor
44
from example.serializers import (
5-
BlogSerializer, EntrySerializer, AuthorSerializer, CommentSerializer)
5+
BlogSerializer, EntrySerializer, AuthorSerializer, CommentSerializer,
6+
RenamedAuthorSerializer)
67

78

89
class BlogViewSet(viewsets.ModelViewSet):
@@ -21,6 +22,11 @@ class AuthorViewSet(viewsets.ModelViewSet):
2122
serializer_class = AuthorSerializer
2223

2324

25+
class RenamedAuthorViewSet(viewsets.ModelViewSet):
26+
queryset = RenamedAuthor.objects.all()
27+
serializer_class = RenamedAuthorSerializer
28+
29+
2430
class CommentViewSet(viewsets.ModelViewSet):
2531
queryset = Comment.objects.all()
2632
serializer_class = CommentSerializer
@@ -41,4 +47,3 @@ class CommentRelationshipView(RelationshipView):
4147
class AuthorRelationshipView(RelationshipView):
4248
queryset = Author.objects.all()
4349
self_link_view_name = 'author-relationships'
44-

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /