python 2.7 - Inverting the ModelSerializer order with Django-Rest-Framework -
using python v2.7, django 1.10, drf 3.x related other pending question at: including data in django intermediate table in django-rest-framework serializer i'm getting set of serializers i've built:
{ "id": 11, "asset_number": "1234", "serial_number": "1234", "name": "instrument1", "checksum_string": "0123456789abcdef0123", "instr_type": { "id": 70, "make": "make1", "model": "model1", "service_email": "model1@company.com", "service_website": "www.model1.com" }, "validated_versions": [{ "id": 9, "method": { "id": 9, "name": "method1", "description": "method 1 description" }, "version_name": "123", "cmd_line_script": "script", "sop": "sop" }, { "id": 10, "method": { "id": 10, "name": "method2", "description": "method 2 description" }, "version_name": "123", "cmd_line_script": "script", "sop": "sop" }, { "id": 12, "method": { "id": 10, "name": "method2", "description": "method 2 description" }, "version_name": "456", "cmd_line_script": "script", "sop": "sop" }] }
but notice there's lot of redundancy in data, want have show method
child of instrument
, methods
have version
associated instrument
being serialized. want versions
of method
listed children of method
, excluding versions
of method
not validated on instrument
. want instead:
{ "id": 11, "asset_number": "1234", "serial_number": "1234", "name": "instrument1", "checksum_string": "0123456789abcdef0123", "instr_type": { "id": 70, "make": "make1", "model": "model1", "service_email": "model1@company.com", "service_website": "www.model1.com" }, "validated_methods": [{ "id": 9, "name": "method1", "description": "method 1 description", "valid_versions": [{ "id": 9, "version_name": "123", "cmd_line_script": "script", "sop": "sop" }] }, { "id": 10, "name": "method2", "description": "method 2 description", "valid_versions": [{ "id": 10, "version_name": "123", "cmd_line_script": "script", "sop": "sop" }, { "id": 12, "version_name": "456", "cmd_line_script": "script", "sop": "sop" }] }] }
here's serializers currently, in case helps:
class userserializer(serializers.modelserializer): class meta: model = settings.auth_user_model excludes = 'password' class userprofileserializer(serializers.modelserializer): #user = serializers.serialize(settings.auth_user_model, read_only=true) user = userserializer(read_only=true) class meta: model = userprofile.user #fields = ('last_login', 'is_superuser', 'username', 'first_name', 'last_name', 'email', 'is_staff', 'is_active', 'date_joined') fields = '__all__' #excludes = ('password',) class instrtypeserializer(serializers.modelserializer): class meta: model = instrtype fields = '__all__' class methodserializer(serializers.modelserializer): description = serializers.readonlyfield(source='descripton') class meta: model = method fields = ('id', 'name', 'description') class versionserializer(serializers.modelserializer): method = methodserializer(read_only=true) #instr_version = instr_versionserializer(source='instr_version_set', many=true, read_only=true) class meta: model = version fields = ('id', 'method')#, 'version_number', 'cmd_line_script', 'sop') class instr_to_versionserializer(serializers.modelserializer): version = versionserializer(source='fk_version', read_only=true, many=true) validator = userprofileserializer(source='validating_user.user', read_only=true) #validator = serializers.readonlyfield(source='self.validating_user.user.email') version_name = serializers.readonlyfield(source='version_number') cmd_line_script = serializers.readonlyfield() sop = serializers.readonlyfield() method = methodserializer(source='fk_method',read_only=true) time_validated = serializers.readonlyfield(source= 'timestamp') class meta: model = instr_version fields = ('id', 'version', 'validator', 'time_validated', 'method', 'version_name', 'cmd_line_script', 'sop') class instrumentserializer(serializers.modelserializer): instr_type = instrtypeserializer(source='fk_instr_type', read_only=true) validated_versions = instr_to_versionserializer(source='versionsfrominstrument', many=true, read_only=true) class meta: model = instrument fields = ('id', 'asset_number', 'serial_number', 'name', 'checksum_string', 'instr_type', 'validated_versions')
thanks in advance, everyone.
Comments
Post a Comment