Created
July 11, 2017 20:20
-
-
Save haikuginger/13e3eefa5a0faa289074a02b48a3408e to your computer and use it in GitHub Desktop.
DRF field compatibility
This file contains hidden or 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
""" | |
Utilities for working with ConfigurationModels. | |
""" | |
from __future__ import unicode_literals, absolute_import | |
from django.apps import apps | |
from django.contrib.auth.models import User | |
from rest_framework.parsers import JSONParser | |
from rest_framework.serializers import ModelSerializer | |
try: | |
from rest_framework.serializers import ALL_FIELDS | |
except ImportError: | |
ALL_FIELDS = None | |
def get_serializer_class(configuration_model): | |
""" Returns a ConfigurationModel serializer class for the supplied configuration_model. """ | |
class AutoConfigModelSerializer(ModelSerializer): | |
"""Serializer class for configuration models.""" | |
class Meta(object): | |
"""Meta information for AutoConfigModelSerializer.""" | |
model = configuration_model | |
if ALL_FIELDS: | |
fields = ALL_FIELDS | |
def create(self, validated_data): | |
if "changed_by_username" in self.context: | |
validated_data['changed_by'] = User.objects.get(username=self.context["changed_by_username"]) | |
return super(AutoConfigModelSerializer, self).create(validated_data) | |
return AutoConfigModelSerializer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment