access parent form's cleaned_data from inline form clean()
Asked Answered
H

1

7

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, but is it possible to be done in forms.py for both the frontend and the admin?

Hippodrome answered 16/4, 2012 at 23:25 Comment(0)
Y
8

No, these forms are separate objects and are completely unaware of each other. But you are providing same data to all forms, so you should be able to check fields from self.data.

Yardage answered 17/4, 2012 at 4:13 Comment(5)
Thank you for your reply. I'm not sure I am following. When I look at the cleaned_data for the inline form, I am only seeing the data for that form. How should I access the fields from the parent form?Hippodrome
Use self.data, not self.cleaned_dataYardage
Excellent. I did not know that was available. I've only ever used cleaned_data. Thank you for your help.Hippodrome
You should not use form.data everywhere because it is raw data and it should be checked and cleaned but in cases like "check if the value exists" or "check if the value is equal to something" it works fine.Yardage
I wasn't going to use it for all data just to check for a value. cleaned_data will be what is used for the form's data. Thanks again.Hippodrome

© 2022 - 2024 — McMap. All rights reserved.