Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure if I was just being a clutz but the following worked (and answers my whole question):</p> <p>class OrderForm(ModelForm): class Meta: model = Order</p> <pre><code>def clean_shipping_street(self): print 'VALIDATING!!! YEY!' if self.cleaned_data['ship_to_billing']: return self.clean_billing_street() return super(OrderForm, self).clean_shipping_street() </code></pre> <p>But if you think I'm going about this the wrong way, please let me know! </p> <p>As Nick points out below, cleaned_data isn't filled in a guaranteed order, meaning <code>ship_to_billing</code> might not exist when <code>clean_shipping_street()</code> is called. The way around this is to call the <code>clean_shipping_street()</code> method instead accessing <code>cleaned_data</code>.</p> <pre><code>def clean_shipping_street(self): print 'VALIDATING!!! YEY!' if self.clean_ship_to_billing(): return self.clean_billing_street() return super(OrderForm, self).clean_shipping_street() </code></pre> <p>If you weren't as lazy as I was when I wrote the code, you might want to avoid so many duplicate validations of the boolean field. This should be faster (provided the default field isn't run unless it's needed - not sure on that myself):</p> <pre><code>def clean_shipping_street(self): print 'VALIDATING!!! YEY!' if self.cleaned_data.get('ship_to_billing', self.clean_ship_to_billing): return self.clean_billing_street() return super(OrderForm, self).clean_shipping_street() </code></pre> <p><strong><em>OR</em></strong> <em>even</em> better than that:</p> <pre><code>def clean_shipping_street(self): if not self.cleaned_data.has_key['ship_to_billing']: self.cleaned_data['ship_to_billing'] = self.clean_ship_to_billing() if self.cleaned_data['ship_to_billing']: return self.clean_billing_street() return super(OrderForm, self).clean_shipping_street() </code></pre> <p>It's only slightly different but it should mean clean_ship_to_billing() gets called a lot less than my previous efforts. But seriously, I doubt you could even detect these "improvements" in a profiling session.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload