Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deserialize is not called for null values #1255

Open
Ted-Barrett opened this issue Sep 8, 2024 · 1 comment
Open

deserialize is not called for null values #1255

Ted-Barrett opened this issue Sep 8, 2024 · 1 comment

Comments

@Ted-Barrett
Copy link

Ted-Barrett commented Sep 8, 2024

I would like to handle null values when deserializing. I have the following custom attribute:

class TestAttribute(Attribute):
    attr_type = BINARY

    def serialize(self, value: Any) -> Any:
        return json.dumps(value)
    def deserialize(self, value: Any) -> Any:
        raise RuntimeError("STOP!!")

being used in a model:

class MyModel(Model):
    test_field = TestAttribute(null=True)

If the field has data in it, I get the runtime error STOP!! as expected.

If the field is empty, there is no such error, and e.test_field is None.

How can I handle the behaviour when a value is null?

@ikonst
Copy link
Contributor

ikonst commented Sep 16, 2024

Unfortunately this check simply won't let you have that:

attribute_value = attribute_values.get(attr.attr_name)
if attribute_value and NULL not in attribute_value:

(Curiously, the NULL DynamoDB attribute value would also have the same effect, making it indistinguishable from absence of value. A shame.)

Typically I'd override a model's deserialize if I want the opportunity to work with the raw data before field-by-field deserialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants