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

[Resolved] How to override the to python and to db serialization and deserialization? #74

Closed
ghost opened this issue Nov 7, 2022 · 3 comments

Comments

@ghost
Copy link

ghost commented Nov 7, 2022

For my specific use case, I am combining your library with https://github.com/s-knibbs/dataclasses-jsonschema
So, I am trying to have it serialize/deserialize to/from the dataclass.

In the python code, I want to get a dataclass instance when I access .field and I also want to be able to do .field = dataclass_instance

I do not want to affect the behavior on Django Admin.

My attempt at overriding:

But it doesn't seem to do anything for the python code. MyModel.objects.first().my_field still returns a dict

class JSONFormField(JSONField):

    def __init__(self, dataclass_cls, **kwargs):
        super().__init__(schema=dataclass_cls.json_schema(), **kwargs)

    def to_python(self, value):
        raise ValueError('hello')
        return value

    def get_prep_value(self, value):
        raise ValueError('hello2')
        return value
@ghost ghost changed the title [Question] How to override the to python and to db serialization and deserialization? [Resolved] How to override the to python and to db serialization and deserialization? Nov 7, 2022
@ghost
Copy link
Author

ghost commented Nov 7, 2022

Ah, I was looking at the wrong thing

Should be

class JSONFormField(JSONField):

    def __init__(self, dataclass_cls, **kwargs):
        super().__init__(schema=dataclass_cls.json_schema(), **kwargs)

    def from_db_value(self, value, expression, connection):
        print('what up')
        return value

    def get_prep_value(self, value):
        print('hello')
        return value

@ghost ghost closed this as completed Nov 7, 2022
@bhch
Copy link
Owner

bhch commented Nov 7, 2022

Since you're going to use a library to generate the schema, I must alert you that django-jsonform doesn't support all the standard JSON schema features. We've implemented a custom spec which is more suitable for form generation, rather than JSON data validation.

For example, listing required fields in an array is not supported. To make a field required, you have to set the required keyword inside the field:

# won't work
{
    ...
    'required': ['field1', 'field2', ...]
}

---

# will work
{
    ...
    'field1': {'required': True},
    'field2': {'required': True}
} 

Although, the final goal is to fully support the standard schema, but it's still quite some way off.

@dongyuzheng
Copy link

I am @ghost

This issue was closed.
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