Found a workaround.
the field get_value uses the self.fieldname, in this particular specific
case i dont want that.. i want it to get the value from the source (json)
```
class BaseExtractDataFromJsonField(serializers.Field):
"""
Instead of using the field_name, for the value rather use the source
as then we can massage ugly json data
"""
def get_value(self, dictionary):
if self.source not in dictionary:
if getattr(self.root, 'partial', False):
return serializers.empty
# We override the default field access in order to support
# lists in HTML forms.
if serializers.html.is_html_input(dictionary):
return dictionary.getlist(self.source)
return dictionary.get(self.source, serializers.empty)
class CharField(BaseExtractDataFromJsonField, serializers.CharField): pass
class EmailField(BaseExtractDataFromJsonField, serializers.EmailField): pass
class DecimalField(BaseExtractDataFromJsonField, serializers.DecimalField):
pass
class IntegerField(BaseExtractDataFromJsonField, serializers.IntegerField):
pass
and then simply use the overridden CharField above..
```
On Monday, April 30, 2018 at 6:38:21 PM UTC+2, Ross Crawford-d'Heureuse
wrote:
>
> Hi there,
>
> Successfully installed djangorestframework-3.8.2
>
> ```
> class CustomerCardSerializer(serializers.Serializer):
> customer_id = serializers.CharField(source='customerID')
> customer_uid = serializers.CharField(source='CustomerUID')
> email = serializers.EmailField(source='CustomerEmail')
> card = serializers.CharField(source='KundenKarteNr')
> ```
>
>
> ```
> from blah.apps.default.serializers import CustomerCardSerializer
>
> cc=CustomerCardSerializer(data={'MobileDC': None, 'CustomerUID':
> '88B6F5AA-3D7E-2EBE-B968-F73C3992083D', 'BasePoints': '10',
> 'KundenKarteNr': '20088400056241905', 'KundenKarteKennz': 'DC',
> 'CardStatus': '0', 'WebserviceOffline': 'false', 'CustomerEmail': None,
> 'TotalPoints': '10', 'MobileDCStatus': None, 'CardType': '1',
> 'UsableAccount': '296', 'CustomerScan': 'true', 'CurrentAccount': '296',
> 'WebserviceTransaktionsID': '000201721020171804301730',
> 'WebserviceErrorId': None, 'BlockedAccount': '0', 'customerID': '5261',
> 'ExtraPoints': '0.0', 'PreviousCardNr': None})
> >>> cc.is_valid()
> False
> >>> cc.errors
> {'customer_id': [ErrorDetail(string=u'This field is required.',
> code=u'required')], 'customer_uid': [ErrorDetail(string=u'This field is
> required.', code=u'required')], 'email': [ErrorDetail(string=u'This field
> is required.', code=u'required')], 'card': [ErrorDetail(string=u'This field
> is required.', code=u'required')]}
> ```
>
> If I change the serializer fieldnames to the UGLY field names works
> fine... but this is not what we want..
>
> Has anyone been aware of new changes that may affect this in the 3.8.2
> release?
>
> Thanks in advance.
>
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.