-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from AltSchool/feature/auto-link-objects
auto-generate link objects for non-sideloaded relations
- Loading branch information
Showing
9 changed files
with
155 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
def merge_link_object(serializer, data, instance): | ||
"""Add a 'links' attribute to the data that maps field names to URLs. | ||
NOTE: This is the format that Ember Data supports, but alternative | ||
implementations are possible to support other formats. | ||
""" | ||
|
||
link_object = {} | ||
|
||
link_fields = serializer.get_link_fields() | ||
for name, field in link_fields.iteritems(): | ||
# For included fields, omit link if there's no data. | ||
if name in data and not data[name]: | ||
continue | ||
|
||
# Default to DREST-generated relation endpoints. | ||
link = getattr(field, 'link', "/%s/%s/%s/" % ( | ||
serializer.get_plural_name(), | ||
instance.pk, | ||
name | ||
)) | ||
if callable(link): | ||
link = link(name, field, data, instance) | ||
|
||
link_object[name] = link | ||
|
||
if link_object: | ||
data['links'] = link_object | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,7 @@ | |
ROOT_URLCONF = 'tests.urls' | ||
|
||
BASE_DIR = os.path.dirname(__file__) | ||
|
||
DYNAMIC_REST = { | ||
'ENABLE_LINKS': True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters