Discussion:
nice_name = serializers.CharField(source='Uglyfield') no longer working
Ross Crawford-d'Heureuse
2018-04-30 16:38:21 UTC
Permalink
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 django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ross Crawford-d'Heureuse
2018-05-02 06:56:24 UTC
Permalink
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
Post by Ross Crawford-d'Heureuse
Hi there,
Successfully installed djangorestframework-3.8.2
```
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
'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 django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
s***@gmail.com
2018-05-04 08:04:36 UTC
Permalink
I created a simply DRF plugin to solve this issue

https://github.com/rosscdh/drf-prettify-json_serializer-field


On Wednesday, May 2, 2018 at 8:56:24 AM UTC+2, Ross Crawford-d'Heureuse
Post by Ross Crawford-d'Heureuse
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)
```
"""
Instead of using the field_name, for the value rather use the source
as then we can massage ugly json data
"""
return serializers.empty
# We override the default field access in order to support
# lists in HTML forms.
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
Post by Ross Crawford-d'Heureuse
Hi there,
Successfully installed djangorestframework-3.8.2
```
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
'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 django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...