Skip to content

Instantly share code, notes, and snippets.

@Grokzen
Created June 8, 2018 12:34
Show Gist options
  • Save Grokzen/affc6c37715c80e24932325c3872ef59 to your computer and use it in GitHub Desktop.
Save Grokzen/affc6c37715c80e24932325c3872ef59 to your computer and use it in GitHub Desktop.
#
# extras.urls
#
# Custom Field choices
router.register(r'_custom_field_choices', views.CustomFieldChoicesViewSet, base_name='field-choice')
#
# extras.api.views
#
class CustomFieldChoicesViewSet(ViewSet):
"""
"""
permission_classes = [IsAuthenticatedOrLoginNotRequired]
def __init__(self, *args, **kwargs):
super(CustomFieldChoicesViewSet, self).__init__(*args, **kwargs)
self._fields = OrderedDict()
for cfc in CustomFieldChoice.objects.all():
self._fields.setdefault(cfc.field.name, [])
self._fields[cfc.field.name].append({'value': cfc.pk, 'label': cfc.value})
def list(self, request):
return Response(self._fields)
def retrieve(self, request, pk):
if pk not in self._fields:
raise Http404
return Response(self._fields[pk])
def get_view_name(self):
return "Custom Field choices"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment