[Answer]-Django: How to pass values into value_from_datadict

1đź‘Ť

You can see this in the source: it’s not very complicated, it’s passed the data dict and a name parameter and by default it just gets the value from the key corresponding to the name.

If you’re after something a bit more complex, perhaps the relevant code from MultiWidget might be useful: there it just calls value_from_datadict for each child widget.

If you’re still having trouble, you should probably paste the code for your widget.

Edit

There are a large number of things wrong with your code, but basically this is utterly the wrong approach.

I have no idea what you mean by “storing the data in the dict” – that’s not what value_from_datadict is for at all, it’s for returning a value to display in the widget – and I also can’t imagine why you want to “serialize” the result, but the problems run much deeper than that.

Your widget should not be doing any of the things it is doing. Most specifically, it should not be querying the database and outputting inputs for each row. And it should not be including both checkbox and text input for each row, either. Normally if you wanted to do that you would use a MultiWidget as I mentioned above, but that’s not really appropriate here either because of the need for multiple rows.

What you actually need here is not a custom widget at all, or even a custom field, but simply a formset. Each form in the formset can relate to a single MyData instance, and you can have a textfield and a booleanfield for each.

Leave a comment