Setting initial Django form field value in the __init__ method

2024/11/19 5:44:46

Django 1.6

I have a working block of code in a Django form class as shown below. The data set from which I'm building the form field list can include an initial value for any of the fields, and I'm having no success in setting that initial value in the form. The if field_value: block below does indeed populate the initial form dictionary attribute, but the initial value is not being displayed. Note that (in case you are wondering) the .initial attribute does not exist until after the super() call.

Can this be done?

If so, what I'm not doing right to make this work?

Thanks!

def __init__(self, *args, **kwargs):id = kwargs.pop('values_id', 0)super(LaunchForm, self).__init__(*args, **kwargs)# Lotsa code here that uses the id value# to execute a query and build the form# fields and their attributes from the # result setif field_value:self.initial[field_name] = field_value
Answer

I had that exact same problem and I solved it doing this:

def __init__(self, *args, **kwargs):instance = kwargs.get('instance', None)kwargs.update(initial={# 'field': 'value''km_partida': '1020'})super(ViagemForm, self).__init__(*args, **kwargs)# all other stuff
https://en.xdnf.cn/q/26475.html

Related Q&A

Should I use a main() method in a simple Python script?

I have a lot of simple scripts that calculate some stuff or so. They consist of just a single module.Should I write main methods for them and call them with the if __name__ construct, or just dump it a…

Where can I find mad (mean absolute deviation) in scipy?

It seems scipy once provided a function mad to calculate the mean absolute deviation for a set of numbers:http://projects.scipy.org/scipy/browser/trunk/scipy/stats/models/utils.py?rev=3473However, I c…

Map of all points below a certain time of travel?

My question is very simple and can be understood in one line:Is there a way, tool, etc. using Google Maps to get an overlay of all surface which is below a certain time of travel?I hope the question i…

Pandas populate new dataframe column based on matching columns in another dataframe

I have a df which contains my main data which has one million rows. My main data also has 30 columns. Now I want to add another column to my df called category. The category is a column in df2 which co…

Remove an imported python module [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Unload a module in Python After importing Numpy, lets say I want to delete/remove numpy import referenceimport sys import…

Should I create each class in its own .py file?

Im quite new to Python in general.Im aware that I can create multiple classes in the same .py file, but Im wondering if I should create each class in its own .py file.In C# for instance, I would have a…

Should you put quotes around type annotations in python

Whats the difference between these two functions? Ive seen people put quotes around type annotations and other times leave them out but I couldnt find why people choose to use one or the other.def do_…

Handling GET and POST in same Flask view

When I type request.form["name"], for example, to retrieve the name from a form submitted by POST, must I also write a separate branch that looks something like request.form.get["name&qu…

Overriding inherited properties’ getters and setters in Python

I’m currently using the @property decorator to achieve “getters and setters” in a couple of my classes. I wish to be able to inherit these @property methods in a child class.I have some Python code …

Python Message Box Without huge library dependency

Is there a messagebox class where I can just display a simple message box without a huge GUI library or any library upon program success or failure. (My script only does 1 thing). Also, I only need it …