views:

320

answers:

1

I've got a Supplier Invoice (SupplierInvoice) parent model that holds a number of orders (SupplierOrder). Right now if the user puts together an invoice via django admin, django checks to see if there are price matches for that Supplier and Product in a cost price table and pulls through the respective fields. This process happens on a custom save method.

What I need to add is code to cover any instances where there is no price match, in which case validation should kick in and warn the user that they have to enter something into the order before committing.

I've tried several variations on custom modelForm 'clean' methods and they kick in before the save method has a chance to look up prices and check first.

Ideally what I want is Django to check the appropriate tables for price matches THEN validate to say there are no matches and one must be entered.

http://dpaste.com/hold/47848/ for the corresponding code.

Thanks for any help - I'm totally stumped!

+1  A: 

Well yes, clean happens before save - as it must, to check that the form is valid before saving it. So you will need to do the price lookup in the clean.

Daniel Roseman
If you want to keep the price-lookup behavior in the model layer as well (in case you save instances of that model without the use of Forms), you could refactor it into a separate method that you call from the form's clean method, and also from the model's save() method if the price hasn't already been set.
Carl Meyer
Defo the answer but have had mixed success with this approach. I'm having difficulty accessing one variable of the the inline form's parent that makes the price-lookup do the business. I think this is because the parent (invoice) hasn't been saved yet and thus the variable is not accessible. It is present in request.post - how do I grab a request post variable in a clean method? Thanks