2014-04-11 22:43 GMT+02:00 Cédric Krier <[email protected]>: > On 10 Apr 12:49, Jesús Martín Jiménez wrote: > > 2014-04-10 12:40 GMT+02:00 Jesús Martín Jiménez <[email protected] > >: > > > > > > > > > > > > > > 2014-04-10 12:36 GMT+02:00 Cédric Krier <[email protected]>: > > > > > > On 10 Apr 12:25, Jesús Martín Jiménez wrote: > > >> > 2014-04-10 11:58 GMT+02:00 Cédric Krier <[email protected]>: > > >> > > > >> > > On 09 Apr 05:29, jmartin wrote: > > >> > > > Hi, > > >> > > > > > >> > > > I'm trying to add a domain in the product field os sale.line > that is > > >> > > filled > > >> > > > from the sale.sale form view, in order to avoid make sales of > some > > >> > > products > > >> > > > grouped by their categories to some parties. This is the code: > > >> > > > > > >> > > > class SaleLine: > > >> > > > __name__ = 'sale.line' > > >> > > > product_domain = fields.Function(fields.One2Many( > > >> > > > 'product.product', None, 'Product Category Domain', > > >> > > > on_change_with=['_parent_sale.party', 'product'], > > >> > > # Fixed > > >> > > > > > >> > > depends=['_parent_sale.party']), > > >> > > # Fixed > > >> > > > 'on_change_with_product_domain') > > >> > > > > > >> > > > @classmethod > > >> > > > def __setup__(cls): > > >> > > > super(SaleLine, cls).__setup__() > > >> > > > product_domain = ('id', 'not in', Eval('product_domain', > > >> [])) > > >> > > > if product_domain not in cls.product.domain: > > >> > > > cls.product.domain.append(product_domain) > > >> > > > > > >> > > > def on_change_with_product_domain(self, name=None): > > >> > > > pool = Pool() > > >> > > > ProductCategory = > pool.get('party.party-product.category') > > >> > > > Template = pool.get('product.template') > > >> > > > party = self.sale.party > > >> > > > product_categories = ProductCategory.search([('party', > '=', > > >> > > party)]) > > >> > > > products = [] > > >> > > > for product_category in product_categories: > > >> > > > templates = Template.search([ > > >> > > > ('category', '=', > product_category.category)]) > > >> > > > for template in templates: > > >> > > > for product in template.products: > > >> > > > products.append(product.id) > > >> > > > return products > > >> > > > > > >> > > > * In other file there is defined the m2m record that relates > party > > >> with > > >> > > > product.category > > >> > > > > > >> > > > The module avoids that a product will be sold to the customer > if the > > >> > > > product belongs to any of the categories listed for this > customer. > > >> But it > > >> > > > does this in a strange way. I can select all products of the > list > > >> in the > > >> > > > sale.line record, and when I'm going to save the sale.sale > record, > > >> tryton > > >> > > > says me that the field product of sale.line is not correct > > >> accordingly > > >> > > with > > >> > > > its domain. I would like that products which I can not sell to > the > > >> > > > customer, will be excluded from those I can select, but how can > I do > > >> > > that? > > >> > > > > >> > > So I guess the product_domain is not correctly filled by the > > >> on_change. > > >> > > You should check the value of this field in the UI. > > >> > > > > > The field is empty until I save the record with a correct product, in > which > > case, it puts the correct value. > > So it seems the on_change_with is not correctly defined because it is > not called. >
IMHO I think the problem is another. When the party changes there isn't any instance of sale.line to call. I mean, as the on_change_with() method is a instance method, it is not called, because there isn't any instance yet. When I create a sale.line, the party is already changed, so the on_change_with() method is not called anymore. I've tried to call it as a default method of the function field, but I need a instance of sale.line to apply it in order to climb to its parent sale field, and as default method is a static/class one (there isn't any instance), I can't do that either. I've also tried to change the on_change_with() method to another field. But the problem is that the default focus falls in the product field, so to make it works, I have to change the other field before, and I think this is not the end user can expect. I think a way to solve the problem is to make a call to a instance method when the sale.line is created and the focus goes to the product field, I mean, call the method when the focus goes to the product field. But I don't know how to do that, or whether that is possible. -- Jesús Martín Zikzakmedia SL Dr. Fleming, 28, baixos 08720 Vilafranca del Penedès ☏ 93 890 21 08
