mixxorz / django-service-objects

Service objects for Django
MIT License
337 stars 29 forks source link

Added support for DictField / ListField #59

Closed peterfarrell closed 4 years ago

peterfarrell commented 4 years ago

There is no way using Django Forms to pass around arbitrary dictionaries. The only other way was to override __init__ and pass the dictionary "input" as a kwarg which is a hack. This is a shim to support passing dictionaries to services:

class PDFGenerate(Service):
    context = DictField()

    process(self):
        context = self.cleaned_data['context']

PDFGenerate.execute({
    'context': {'a': 1, 'b': 2}
})

PR includes tests.

jackton1 commented 4 years ago

@peterfarrell Might also take a look at Postgres JSONField form field.

peterfarrell commented 4 years ago

@peterfarrell Might also take a look at Postgres JSONField form field.

Thanks @jackton1 for the suggestion. I thought about it, but given the data I have -- this seems like a better choice. Also, Django Rest Framework has a DictField and a ListField:

https://www.django-rest-framework.org/api-guide/fields/#dictfield

peterfarrell commented 4 years ago

I added support for ListField to have parity.

peterfarrell commented 4 years ago

I am tempted to add additional features akin the ListField and DictField in the Django Rest Framework:

https://www.django-rest-framework.org/api-guide/fields/#listfield

@jackton1 @c17r Let me know your thoughts. I know they are issues related to using DRF serializers in addition to form fields. I think you ever went that direction -- we'd need these fields in the PR to keep parity.

c17r commented 4 years ago

@peterfarrell thanks for the contribution!

peterfarrell commented 4 years ago

@c17r Thanks, let me know when we can get a build on PyPi.

jackton1 commented 4 years ago

I am tempted to add additional features akin the ListField and DictField in the Django Rest Framework:

https://www.django-rest-framework.org/api-guide/fields/#listfield

@jackton1 @c17r Let me know your thoughts. I know they are issues related to using DRF serializers in addition to form fields. I think you ever went that direction -- we'd need these fields in the PR to keep parity.

@peterfarrell Definitely like the idea you could take a look json-models for things like dict/json validation or using child pattern for values validation personally I use SimpleArrayField which enforces the expected child types ATM.

peterfarrell commented 4 years ago

@jackton1 Thanks. I'll put some thought into it. I was leaning towards the DRF parameters just for simplicity assuming that if you're using Django -- you are likely using DRF for REST API / etc.

seanlinehan commented 4 years ago

Hey @c17r, this solves some problems we're running into as well. Any plan to push a new release to PyPi soon?

c17r commented 4 years ago

We need @mixxorz for that I don't have PyPi access for this project.

seanlinehan commented 4 years ago

Ah got it! Good to know for the future. :-)

mixxorz commented 4 years ago

I've just published a new release. 0.7.0