cleaned-data

access parent form's cleaned_data from inline form clean()

依然范特西╮ 提交于 2020-02-02 02:59:08
问题 I have a main form that has an inline-form. Is it possible to access the main form's cleaned_data from the inline-form's clean function? Here is why I am asking. The main form has a field to define if a property is for-sale or to lease. The inline form then displays either a sale price field or fields for the lease amount and deposit. I am trying to validate that if the property is for sale, then the lease and deposit fields should be empty. I can do this in a view for the frontend interface,

how to receive modelform_instance.cleaned_data['ManyToMany field'] in view when form field is ModelMultipleChoiceField?

天涯浪子 提交于 2019-12-13 02:42:54
问题 Here is the situation: I have a model as below: class School(Model): name = CharField(...) Permit model has three objects: School.objects.create(name='school1') # id=1 School.objects.create(name='school2') # id=2 I have another model: Interest(Model): school_interest = ManyToManyField(School, blank=True,) I then build a ModelForm using Interest: class InterestForm(ModelForm): school_interest = ModelMultipleChoiceField(queryset=School.objects.all(), widget=CheckboxSelectMultiple, required

Python - Django - Form choicefield and cleaned_data

帅比萌擦擦* 提交于 2019-12-11 23:24:17
问题 I'm having an issue with how cleaned_data seems to work. I want to store the cleaned_data in a session so I can repopulate the form object later. The problem is my choice fields seems to store the display name of the dropdown not the actual value. For example: <select name="dropdown_element"> <option value="1">Red</option> <option value="2">Blue</option> </select> If I selected Red and used form.cleaned_data['dropdown_element'] I will get the display name Red and not the value of 1. Is there

Django: Save cleaned_data in a session effectively

删除回忆录丶 提交于 2019-12-10 21:30:27
问题 In one of my forms, I am processing the form data and save it in a session variable. So when I run if locationForm.is_valid(): I execute request.session['streetNumber'] = locationForm.cleaned_data['streetNumber'] request.session['postalCode'] = locationForm.cleaned_data['postalCode'] request.session['state'] = locationForm.cleaned_data['state'] request.session['country'] = locationForm.cleaned_data['country'] But this seems very inefficient. I have tried request.session = locationForm.cleaned

Django MultipleChoiceField does not preserve order of selected values

谁都会走 提交于 2019-12-10 15:41:47
问题 I have a Django ModelForm which exposes a multiple choice field corresponding to a many-to-many relation through a model which holds order of selection (a list of documents) as an extra attribute. At the front-end, the field is displayed as two multiple select fields similar to that in admin, one to list available choices and the other holds the selected elements. The form can be saved with the correct selection of elements but they are always in the order of the original order of choices,

How to access data when form.is_valid() is false

爷,独闯天下 提交于 2019-12-03 05:23:50
问题 When I have a valid Django form, I can access the data with form.cleaned_data. But how do I get at the data a user entered when the form is not valid i.e., form.is_valid is false. I'm trying to access forms within a form set, so form.data seems to just give me a mess. 回答1: You can use form.data['field_name'] This way you get the raw value assigned to the field. 回答2: See http://docs.djangoproject.com/en/dev/ref/forms/validation/#ref-forms-validation Secondly, once we have decided that the

How to access data when form.is_valid() is false

家住魔仙堡 提交于 2019-12-02 19:52:39
When I have a valid Django form, I can access the data with form.cleaned_data. But how do I get at the data a user entered when the form is not valid i.e., form.is_valid is false. I'm trying to access forms within a form set, so form.data seems to just give me a mess. Dmitry Risenberg You can use form.data['field_name'] This way you get the raw value assigned to the field. See http://docs.djangoproject.com/en/dev/ref/forms/validation/#ref-forms-validation Secondly, once we have decided that the combined data in the two fields we are considering aren't valid, we must remember to remove them