jrief / django-entangled

Edit JSON-Model Fields using a Standard Django Form.
MIT License
127 stars 11 forks source link

Entangled field having list of data #19

Open dineshh912 opened 2 years ago

dineshh912 commented 2 years ago

Entangled Form is working great so far, consider below models.py

models.py

class Customer(models.Model):
    name = forms.CharField(max_length=255)
    address = forms.JsonField()

The out put i am looking forward to is

{'name ': 'john doe', 'address': [ {'city': 'zxc', 'zipcode': 'zxc'}, {'city': 'zxc', 'zipcode': 'zxc'} ] }

Is there a way to get above output using entangled forms? I tried to combine entangled forms with formset_factory()

below is output i am getting when combine with formset_factory()

[{'name ': 'john doe', 'address': {'city': 'zxc', 'zipcode': 'zxc'} }, {'name ': 'john doe', 'address': {'city': 'zxc', 'zipcode': 'zxc'} }, {'name ': 'john doe', 'address': {'city': 'zxc', 'zipcode': 'zxc'} }]

forms.py

class testForm(EntangledModelForm):
    city = forms.CharField(max_length=255, required=True)
    zipcode = forms.CharField(max_length=34, required=True)

    class Meta:
        model = Customer
        entangled_fields = {"address": ['city', 'zipcode'] }
        untangled_fields = ['name']

testFormSet = formset_factory(testForm, extra=1)
nikolas-dev commented 2 years ago

Must necessary. I basically need this but can not explain to the maintainer https://github.com/jrief/django-entangled/issues/9

jrief commented 2 years ago

I never tested django-entangled with formset_factory.

Something you can try is my other project django-formset. This offers the same functionality as formset_factory, but IMHO better. The form returns JSON and it should work together with django-entangled. I have to add some more tests though.

dineshh912 commented 2 years ago

Will give a try to django-formset, thanks

nikolas-dev commented 2 years ago

@dineshh912 Any update?

CleitonDeLima commented 1 year ago

The right in this case would not be better to use modelformset-factory?

morlandi commented 1 day ago

It would be interesting to collect a series of use cases where the layout of the JSON requested by the application is known, and a snippet on how to do it with django-entangled is suggested.

In my case, for example, the requested JSON must have this structure:

[
    {
        "available": true,
        "ingredients": {
            "235647f2-a181-44c2-82fb-df1578fa61ef": 28.6,
            "3fe6e4b4-9e1a-4dc8-b3e6-560538922878": 2.77,
            "77b84ae0-185f-45f3-8a01-8b8a70fda9df": 5.2,
            "c67fc924-e8ca-46d0-8f3a-009ac6df5d1f": 10.0
        },
        "timestamp": "2024-09-07"
    },
    {
        "available": true,
        "ingredients": {
            "235647f2-a181-44c2-82fb-df1578fa61ef": 66.67,
            "3fe6e4b4-9e1a-4dc8-b3e6-560538922878": 3.2,
            "77b84ae0-185f-45f3-8a01-8b8a70fda9df": 10.0,
            "c67fc924-e8ca-46d0-8f3a-009ac6df5d1f": 1.13
        },
        "timestamp": "2024-09-07"
    }
]

In this example, the uuid refers to the primary key of a small lookup table.

I'm already using a solution involving a BaseFormSet with django.forms.formsets.formset_factory, and using a modal window and some not-so-exciting Javascript. The result is satisfactory but I'd like to use a more declarative approach

formula_editor

I'd like to evaluate django-entangled because I have a feeling that it might be the right path (which in my case means more Django code and less javascript ;) ) but I don't know where to start and any suggestions would be appreciated.

P.S. Should some javascript still be needed, it will be vanilla-js, or even better I'ld rather hide it under HTMX

jrief commented 1 day ago

You may also try my follow-up project. There one can use collections of form inside of dialogs.

morlandi commented 1 day ago

Thank you @jrief , django-formset was already on my list of interesting django apps to study and evaluate. Thank you for pointing out that specific support for Modal and Model Modals has been added; I've really interested in this topic too.

I'm currently applying my own solution to cope with form validation in modals: https://github.com/morlandi/django-htmx-forms

It does fill the bill both in the Django admin and in any custom front-end, being agnostic of the surrounding context; however, it's somehow incomplete and I wouldn't mind replacing it with something more appropriate

Hopefully, I will find sooner or later the time required to take a closer look at django-formset

The only concern is that it might be too complex a solution; lately, I'm becoming almost maniacal in trying in every way to file down everything I can to produce minimalist solutions.

jrief commented 17 hours ago

The only concern is that it might be too complex a solution; lately, I'm becoming almost maniacal in trying in every way to file down everything I can to produce minimalist solutions.

django-formset attempts to bring the form logic of Django to the browser, so that developers only have to focus on the business logic in the backend, without sacrifying the user experience modern browsers offer. So this tool is more for developers of ERP applications rather than games. Use it if you want to focus your busines complexity just on the backend.