Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grabbing POST variable in form clean() method

Is there a straightforward way to access a POST variable to complete some custom validation lookups in a admin form field's clean method?

def clean_order_price(self):
  if not self.cleaned_data['order_price']:
    try:
      existing_price = ProductCostPrice.objects.get(supplier=supplier, product_id=self.cleaned_data['product'], is_latest=True)
    except ProductCostPrice.DoesNotExist:
      existing_price = None
    if not existing_price:
      raise forms.ValidationError('No match found, please enter new price')
    else:
      return existing_price.cost_price_gross
  else:
      return self.cleaned_data

What I need to grab is the 'supplier' post variable which is not in this form's cleaned data because the supplier field is part of the parent form. Only way I can see of grabbing it is accessing request.POST but not having much success there.

Thank-you

like image 754
user108791 Avatar asked Mar 01 '23 15:03

user108791


1 Answers

The POST data is contained in self.data.

like image 163
Daniel Roseman Avatar answered Mar 05 '23 18:03

Daniel Roseman