Dynamically Update a Form in Django AFTER Initial Creation/Display - django

Is there away to dynamically modify and validate a Django form AFTER it's been created and displayed.(I have found a few snippets that show dynamic form creation, but these require that the dynamic fields are known/defined prior to creating the form.) My requirement is different.
Use Case:
I have a form where I want to display and validate additional input fields based on the selection from a dropdown on the initial form. Based on the selection additional fields are 1) added and 2)must then be validated with appropriate error handling.
(In case you are wondering the data elements to be added are a set of name/value pairs stored in csv format in the model, but when displayed they are shown as separate input fields - the input data will be converted to a csv string prior to saving).
I got this partially working using ajax to dynamically add the additional fields to a template, but have not found a way to validate these new fields. I'm not sure if this is a workable approach. I could probably do this in the browser using javascript, but would prefer a Django/server side solution.

OK - So none responded to my question so far. However I did a bit more googling and found an excellent article that describe the solution I was looking for. Major Kudo's to the author of this blog.
Create a Django Dynamic Form with JQuery - Dynamic Field Addition and Removal

Related

Why doesn't Web Forms for Marketers correctly populate the selected DropList value in my custom save action?

Inside my custom save action, I iterate through the form fields (the AdaptedControlResult objects). Any form field which is a DropList (from any source -- manually entered values, or an item lookup) is returning a string value of System.Collections.Generic.List`1[System.String]
Now, it's important to know, it's not returning an actual List. It's returning a literal string with that value.
So, this call:
fields.GetEntryByName("MyFieldName").Value
Is returning the string: System.Collections.Generic.List`1[System.String]. Not the value of the dropdown. A string saying that's it's a List<string>.
I have confirmed the HTML of the form is rendering correctly. I have manually checked the inbound HttpContext.Current.Request.Form values as well...
HttpContext.Current.Request.Form["BGWnjkQqrE6w6sr31IgzrQ.Sections[5].Fields[0].Value"]
That is the correctly-selected value of the drop down (a Sitecore ID).
So, the data is getting output to the form correctly, and the inbound Request.Form data is also correct. Somewhere, Sitecore is deciding not to populate the selected value into the AdaptedControlResult object.
What's additionally odd is that for DropList field types, the selected value does appear in the Parameters property (inexplicably). I would just detect this and use it, but it's not consistent -- for instance, for textbox field types, the word "multiline" appears there.
All other field types work fine -- I have several text entry fields, and some radio button lists. It's just DropList fields.
What is the trick to getting this?
This is a known bug in Sitecore when using the WFFM module in MVC. You can find more details in this Knowledge Base Article - Incorrect data is saved for list fields in WFFM MVC.
The fix is listed on the kb article and depends on the exact version of Sitecore you are using. Sorry to provided a link only answer but there are multiple steps reqiured for the fix and the download for the fix is attached in the article.

Django/Wagtail - Dynamically generate choices based on uploaded document

I am trying to implement a Chart Block in Wagtail where a user can upload a spreadsheet via the DocumentChooserPanel and then a chart is generated based on the data in that spreadsheet.
I currently have it functioning, but the user must explicitly specify the variable they want the chart to be based off of:
class ChartBlock(blocks.StructBlock):
data_file = DocumentChooserBlock()
primary_variable = blocks.CharBlock(required=True,max_length=255)
class Meta:
template = 'dataviz/blocks/Chart.html'
icon = 'cogs'
label = 'Chart'
I was wondering if there is any way to have the primary_variable field dynamically populated with the column headers from the spreadsheet uploaded and stored in Documents - so that the user would be able to choose from a list of available variables instead of having to remember what a variable was called.
Thank you so much!
This wouldn't be easy to achieve with a vanilla implementation of Wagtail.
The page edit form is generated on page load, including the population of choice lists. I'm pretty sure that the same is true of choice lists in StreamField blocks, that they are populated as the block is added.
The code for this hangs between wagtailadmin.edit_handlers, wagtailadmin.views.pages and wagtailcore.blocks. It would be a pretty complex customisation.
Another possible route for investigation would be using the insert_editor_js hook to update the primary_variable field once a document was uploaded. However, I'm not sure you'll find a dependable ID to hang an event listener off of.
However, you will soon be able to custom validate the submitted page. A PR has just been submitted to allow this custom validation. That should reduce the amount of user error when adding the primary_variable.
This feature should be available in Wagtail 1.4, which I believe is due for release pretty soon.
I hope that helps.

Django, custom fields, to_python, and displaying serialized text as-is in Django admin

I have a complex object that I'm storing serialized in a text field. For most purposes, I want the object pulled from the database to be that complex object. However, when I'm editing it in a form, I just want to see that serialized text in the field.
I tried using the value_to_string function but it appears as if it isn't being called at all when editing the record from with admin.
What do I do so that the raw serialized text shows up in the admin text field?
Since the admin already uses the value of your model field, one option would be re-serializing it for editing...
See also formfield_overrides:
This provides a quick-and-dirty way to override some of the Field
options for use in the admin. formfield_overrides is a dictionary
mapping a field class to a dict of arguments to pass to the field at
construction time.
I'm not sure if it's the same problem, but I figured out a way to change the field value in the admin form before displaying it. I explained how to use a custom widget and an custom admin form in this answer.
Note that the custom widget only helps you to display the value in a different format. It won't parse the input value back to an object, although I believe that would be possible too.

Django: How to use modelform on dynamic attributes..?

It's kind of tricky question, It's hard for me to expain what I want so please comment this post what should be edited. Thnx.
So there's a situation where there will be tons of dynamic form inputs. What do I mean by word "dynamic" is that - every attribute (form input) will be stored in table not as column but as value and name of form can be easily edited in the feature with out using migrations. Here's a scheme:
I want to have automatic modelform generated from this scheme. Django could just pick all field names/values/types from table1_attr table. And handle them as it's done with one-column-per-field scheme.
I know that it's possible to add any field later into the model, and then to use "south", but I really afraid of data migration with south. Because I had a lot of segfaults and errors before with this tool. Ofcourse this is an option, and if this scheme will fail - then I'll use usual solution. Btw middleware solutions also fine.
I think all you need is statically defined form in python code and NoSQL storage. As for Redis you can store data in hashes with numbers of keys.

How customize fields in the Django admin interface

I have a model in my Django project with a member which is a char field. Basically data in this field will be entered as comma-separated values.
Without a long-winded explanation of what the overall goal of this is, basically rather than having the admin interface use a simple text field, I'd rather have have some custom HTML for the form so I can just use checkboxes and assemble the values of the checked boxes into a CSV string myself once the form is submitted.
Most of the django customization I was able to find on Google didn't answer my particular problem.
If I understand your question correctly I think you want to search for writing custom widgets. Perhaps start here: http://docs.djangoproject.com/en/dev/topics/forms/