Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Julien Jerphanion
Rex Dri
Commits
c1914a04
Commit
c1914a04
authored
Aug 18, 2018
by
Florent Chehab
Browse files
DictModeViewSet moved to new folder for cleaner code
parent
6222ef22
Changes
3
Hide whitespace changes
Inline
Side-by-side
rex/models/location/location.py
View file @
c1914a04
from
django.db
import
models
from
rest_framework
import
serializers
,
viewsets
,
permissions
from
rex.models.tools
import
DictModeViewSet
# Data model based on : https://unstats.un.org/unsd/methodology/m49/overview/
...
...
@@ -29,25 +30,6 @@ class City(models.Model):
API RELATED STUFF BELLOW
"""
# TODO move somewhere else
class
DictModeViewSet
(
viewsets
.
ModelViewSet
):
"""
ViewSet that renders data as dict with keys corresponding to the model
primary key. Instead of list.
"""
def
list
(
self
,
request
,
*
args
,
**
kwargs
):
response
=
super
(
viewsets
.
ModelViewSet
,
self
).
list
(
# pylint: disable=E1003
request
,
*
args
,
**
kwargs
)
# call the original 'list'
pk_attr_name
=
self
.
serializer_class
.
Meta
.
model
.
_meta
.
pk
.
name
response
.
data
=
{
d
[
pk_attr_name
]:
d
for
d
in
response
.
data
}
return
response
class
CountrySerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
...
...
rex/models/tools/DictModeViewSet.py
0 → 100644
View file @
c1914a04
from
rest_framework
import
viewsets
class
DictModeViewSet
(
viewsets
.
ModelViewSet
):
"""
ViewSet that renders data as dict with keys corresponding to the model
primary key. Instead of list.
"""
def
list
(
self
,
request
,
*
args
,
**
kwargs
):
response
=
super
(
viewsets
.
ModelViewSet
,
self
).
list
(
# pylint: disable=E1003
request
,
*
args
,
**
kwargs
)
# call the original 'list'
pk_attr_name
=
self
.
serializer_class
.
Meta
.
model
.
_meta
.
pk
.
name
response
.
data
=
{
d
[
pk_attr_name
]:
d
for
d
in
response
.
data
}
return
response
rex/models/tools/__init__.py
0 → 100644
View file @
c1914a04
from
.DictModeViewSet
import
DictModeViewSet
# noqa: F401
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment