Prefill form fields of the drop-down-kind

Hi out there,
my understanding of Django and Python is growing day to day but still very small I’,m afraid.
So maybe someone can give me a hint to solve THIS kind of Probelm:

I have several Forms in my Project. I#m on a good way on dealing with forms up to now but encountered an interesting probelm:

Some of the forms are not only used to save data into database but also to ALTER the data. Therefore ich act like this in views (for example):

id_alter = request.POST['alterEmployment']
formdata = Employments.objects.get(id=id_alter)
form = EmploymentsForm(instance=formdata)

so i get a prefilled form with the stored data which i like to change.

BUT i have a Problem with the drop-down fields in the form.
If I want them properly prefilled, I have to go like

work_time_model = forms.ModelChoiceField(queryset=ArbeitszeitModell.objects.
                                             values_list('arbeitszeitmodell', flat=True),

in, but then I get an error message upon save command like: “This is no valid choice”

if i use

work_time_model = forms.ModelChoiceField(queryset=ArbeitszeitModell.objects.all(),

i can save the from with new data but get no proper prefill…

Briefly, what you want to do is alter that field at the time it’s initialized, in the __init__ method of that form.

The issue here is that the form needs to be modified at two separate points in time - first, it needs to be set when the form is first generated for the GET request to get the initial form, then again when the data is posted - and it needs to be done individually for each instance of the form being created.

There have been a couple threads not too long ago that discussed this in more detail. (Using Ajax with forms.CheckboxSelectMultiple and a dependent dropdown is one of them, How to filter the choices in forms.Select widget? is another. Searching the forum for __init__ may help bring up others.)

Thank you for these hints. I tried it with searching but obviously I used not the right search-terms. will study your provided links and hopefully understand the problem…otherwise…i will return with some more questions :wink: