简体   繁体   中英

Django admin custom widget inline processing on adding?

I need to write a complex field+widget, which would be presented by multiple HTML form elements (several INPUTs) and after reading up on the topic, it looks like the most painless way is to subclass the Textarea widget, which would contain a JSON-serialized representation of my field, and use JavaScript to read this value and dynamically create whatever else I need, then update the JSON serialization when a value is modified.

I would love to do it some other way, ie to write out whatever HTML I need in the render() method, and have some other method which would parse the raw POST data to retrieve whatever complex data I need, but it's apparently not possible (?).

Anyway, the first approach works for the normal case of having the fields in the ModelAdmin, but fails when the field is present in an inline. Specifically, I don't understand what creates (clones?) the HTML elements of my field in the inline, and if I could hook up some JavaScript on the "add new inline" event to do the right thing?

a case for the MultiValueField/MultiWidget: https://docs.djangoproject.com/en/1.8/ref/forms/fields/#django.forms.MultiValueField https://docs.djangoproject.com/en/1.8/ref/forms/widgets/#django.forms.MultiWidget

it allows you to "decompress" your value for the widget, and "compress" the values for storing in the field/db. if your MultiWidget needs to be composed of other than standard widgets, you might need to create them too, but it sound like you need "several inputs".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM