Discussion:
Invalid source attribute does not throw an error
andreashanauska via Django REST framework
2018-11-15 15:43:05 UTC
Permalink
Hi,

recently i deployed an API using the following serializers:

class SimpleMotorTypeSerializer(serializers.ModelSerializer):

class Meta:
model = MotorType
fields = ('url',
'id',
'customer_part_number',
)

class MotorSerializer(serializers.ModelSerializer):
motor_type = SimpleMotorTypeSerializer(source='abc', read_only=True)

class Meta:
model = Motor
fields = ('url',
'id',
'motor_type',
...)


The problem is with the source agrument. I just misspelled the source
string (inidicated here with "abc", which does not exist).
My tests didn't throw an error because there is no error thrown at all.

So i am interested how i could avoid this in future (because typing errors
are a human thing). Probably testing each single attribute in my JSON
response.

But mainly i am asking:
Why there is no error raised for a non existing "abc" source?

Greets
Andi
--
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.
Xavier Ordoquy
2018-11-15 17:08:32 UTC
Permalink
Hi,

There are no errors because the value gets map to the instance’s « abc ».
It could be a property with setters / getters that process the value and fill some of the model’s field (or the other way around). For example, you could have a write only property « password » that takes a password, hash/salt it and sets it to the hashed_password field. It could also be some reporting computed live for a read only property.

Regards,
Xavier.
Post by andreashanauska via Django REST framework
Hi,
model = MotorType
fields = ('url',
'id',
'customer_part_number',
)
motor_type = SimpleMotorTypeSerializer(source='abc', read_only=True)
model = Motor
fields = ('url',
'id',
'motor_type',
...)
The problem is with the source agrument. I just misspelled the source string (inidicated here with "abc", which does not exist).
My tests didn't throw an error because there is no error thrown at all.
So i am interested how i could avoid this in future (because typing errors are a human thing). Probably testing each single attribute in my JSON response.
Why there is no error raised for a non existing "abc" source?
Greets
Andi
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
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.
andreashanauska via Django REST framework
2018-11-19 08:31:43 UTC
Permalink
Hi,

ok, understood that. But at some point python would need to call that
property/method whatever. And if it does not exist, python would thrown an
error.
So why this error is not raised?

Regards
Andi
Post by Xavier Ordoquy
Hi,
There are no errors because the value gets map to the instance’s « abc ».
It could be a property with setters / getters that process the value and
fill some of the model’s field (or the other way around). For example, you
could have a write only property « password » that takes a password,
hash/salt it and sets it to the hashed_password field. It could also be
some reporting computed live for a read only property.
Regards,
Xavier.
Le 15 nov. 2018 à 16:43, andreashanauska via Django REST framework <
Hi,
model = MotorType
fields = ('url',
'id',
'customer_part_number',
)
motor_type = SimpleMotorTypeSerializer(source='abc', read_only=True)
model = Motor
fields = ('url',
'id',
'motor_type',
...)
The problem is with the source agrument. I just misspelled the source
string (inidicated here with "abc", which does not exist).
My tests didn't throw an error because there is no error thrown at all.
So i am interested how i could avoid this in future (because typing errors
are a human thing). Probably testing each single attribute in my JSON
response.
Why there is no error raised for a non existing "abc" source?
Greets
Andi
--
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
For more options, visit https://groups.google.com/d/optout.
--
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.
Xavier Ordoquy
2018-11-19 09:47:44 UTC
Permalink
Hi
Hi,
ok, understood that. But at some point python would need to call that property/method whatever. And if it does not exist, python would thrown an error.
It wouldn’t unless you use some specific class that implements that behavior.
... pass
...
Post by andreashanauska via Django REST framework
d = Demo()
d.a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Demo' object has no attribute 'a'
Post by andreashanauska via Django REST framework
d.a = 1
d.a
1
Regards,
Xavier O.
So why this error is not raised?
Regards
Andi
Hi,
There are no errors because the value gets map to the instance’s « abc ».
It could be a property with setters / getters that process the value and fill some of the model’s field (or the other way around). For example, you could have a write only property « password » that takes a password, hash/salt it and sets it to the hashed_password field. It could also be some reporting computed live for a read only property.
Regards,
Xavier.
Post by andreashanauska via Django REST framework
Hi,
model = MotorType
fields = ('url',
'id',
'customer_part_number',
)
motor_type = SimpleMotorTypeSerializer(source='abc', read_only=True)
model = Motor
fields = ('url',
'id',
'motor_type',
...)
The problem is with the source agrument. I just misspelled the source string (inidicated here with "abc", which does not exist).
My tests didn't throw an error because there is no error thrown at all.
So i am interested how i could avoid this in future (because typing errors are a human thing). Probably testing each single attribute in my JSON response.
Why there is no error raised for a non existing "abc" source?
Greets
Andi
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
For more options, visit https://groups.google.com/d/optout <https://groups.google.com/d/optout>.
--
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.
andreashanauska via Django REST framework
2018-11-19 10:44:43 UTC
Permalink
Ok, apparently i didnt understand what you wrote in your first reply. So it
gets mapped during this assignment...
This is the reason it is there and wont throw an error.
Thanks for the clarification!
Hi
Le 19 nov. 2018 à 09:31, andreashanauska via Django REST framework <
Hi,
ok, understood that. But at some point python would need to call that
property/method whatever. And if it does not exist, python would thrown an
error.
It wouldn’t unless you use some specific class that implements that behavior.
... pass
...
Post by Xavier Ordoquy
d = Demo()
d.a
File "<stdin>", line 1, in <module>
AttributeError: 'Demo' object has no attribute 'a'
Post by Xavier Ordoquy
d.a = 1
d.a
1
Regards,
Xavier O.
So why this error is not raised?
Regards
Andi
Post by Xavier Ordoquy
Hi,
There are no errors because the value gets map to the instance’s « abc ».
It could be a property with setters / getters that process the value and
fill some of the model’s field (or the other way around). For example, you
could have a write only property « password » that takes a password,
hash/salt it and sets it to the hashed_password field. It could also be
some reporting computed live for a read only property.
Regards,
Xavier.
Le 15 nov. 2018 à 16:43, andreashanauska via Django REST framework <
Hi,
model = MotorType
fields = ('url',
'id',
'customer_part_number',
)
motor_type = SimpleMotorTypeSerializer(source='abc', read_only=True)
model = Motor
fields = ('url',
'id',
'motor_type',
...)
The problem is with the source agrument. I just misspelled the source
string (inidicated here with "abc", which does not exist).
My tests didn't throw an error because there is no error thrown at all.
So i am interested how i could avoid this in future (because typing
errors are a human thing). Probably testing each single attribute in my
JSON response.
Why there is no error raised for a non existing "abc" source?
Greets
Andi
--
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
For more options, visit https://groups.google.com/d/optout.
--
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
For more options, visit https://groups.google.com/d/optout.
--
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...