django-jsonform icon indicating copy to clipboard operation
django-jsonform copied to clipboard

[Contributing] Thoughts on adding a `contrib` folder for `dataclass` support?

Open ghost opened this issue 3 years ago • 6 comments

Hi @bhch , I'm working on a project which requires django-jsonform + dataclasses-jsonschema.

I feel this project could be quite useful to the Django community. So I want to make this functionality public.

Would you be OK if I make PRs to add a contrib/dataclasses folder?

E.g., from django_jsonform.contrib.dataclasses import DataclassJSONField

ghost avatar Nov 07 '22 16:11 ghost

Could you please describe the use case for DataclassJSONField?

bhch avatar Nov 07 '22 16:11 bhch

In the Python code, we'd work with dataclasse, rather than Dicts.

In the DB, it gets stored as a JSON.

Basically dataclass with a JSON schema get serialized/deserialized as JSON.

ghost avatar Nov 07 '22 17:11 ghost

This makes good sense for django-jsonform because when you require a json schema, then you expect structure. Then the Dataclass would provide structure and utility for the Python side.

ghost avatar Nov 07 '22 17:11 ghost

Pseudo code example:

@dataclass
class Foo:
    x: int

class MyModel:
    foo = DataclassJSONField(Foo)
    foo_list = DataclassJSONField(Foo, many=True)

m = MyModel.objects.first()

assert isinstance(m.foo, Foo)

assert isinstance(m.foo_list, list)
assert isinstance(m.foo_list[0], Foo)

ghost avatar Nov 07 '22 17:11 ghost

That does sound useful. Alright, I'm open to include this in the library. Kindly open a PR. Thank you.

bhch avatar Nov 07 '22 17:11 bhch

I am @ghost

dongyuzheng avatar Nov 11 '22 16:11 dongyuzheng