تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
2 الردود
329 أدوات العرض

We are trying to scrap products but it doesn't list the On Hand location only the default location, I've added a new Many2Many field and trying to limit it there but it's not working. Any ideas on how to get the Stock location field to only show on hand locations on the scrap page?


Here’s what I’ve done:

I created a ManytoMany field in the Scrap model

I put in the related model - stock.location.

I put the product_id, location_id in the dependencies and

I pasted this into the Compute field:

 

available_location_ids = fields.Many2many(

        'stock.location',

        compute='_compute_available_locations',

        string='Available Locations'

    )

 

<field name="location_id" domain="[('product_id', 'in', available_location_ids)]"/>

 

 

it gives a very long error but I think this is the valid part of it:

File "<string>", line 7

    <field name="location_id" domain="[('product_id', 'in', available_location_ids)]"/>

    ^

SyntaxError: invalid syntax

 

What’s wrong with the last line of code?

I did post this previously (thankyou Akhilesh) but I think I've closed it for commenting by accident.


Odoo v18

الصورة الرمزية
إهمال
الكاتب

Komal, that didn't work, I updated it to this:

available_location_ids = fields.Many2many(
'stock.location',
compute='_compute_available_locations',
string='Available Locations'
)
<field name="available_location_ids" invisible="1"/>
<field name="location_id" domain="[('id', 'in', available_location_ids)]"/>

and got

File "<string>", line 7
<field name="available_location_ids" invisible="1"/>
^
SyntaxError: invalid syntax

Thank you for your email, I'm currently out of the office, with no access to phone or email. 

For all product or pricelist requests please resend this email to Alisha Gurung (Alisha.Gurung@gullifood.com.au).

All other enquiries please email Anthony Carageorge (anthony.carageorge@gullifood.com.au) 

regards
Alex
أفضل إجابة

Hi,

available_location_ids is a Many2many field of stock.location, not a list of product IDs.


Please refer to the code:

Python:

from odoo import models, fields, api


class StockScrap(models.Model):

    _inherit = 'stock.scrap'


    available_location_ids = fields.Many2many(

        'stock.location',

        compute='_compute_available_locations',

        string='Available Locations'

    )


    @api.depends('product_id')

    def _compute_available_locations(self):

        for rec in self:

            locations = self.env['stock.quant'].search([

                ('product_id', '=', rec.product_id.id),

                ('quantity', '>', 0),

            ]).mapped('location_id')

            rec.available_location_ids = locations


XML:

<field name="location_id" domain="[('id', 'in', available_location_ids)]"/>


Domain should be ('id', 'in', available_location_ids) instead of ('product_id', 'in', available_location_ids). You don’t need product_id in the domain; the compute already ensures only locations with stock for that product are in the available_location_ids.available_location_ids is a list of stock.location IDs, so compare the ID to it.


Hope it helps.

الصورة الرمزية
إهمال
أفضل إجابة

Hi,
try this
<field name="available_location_ids" invisible="1"/>

<field name="location_id" domain="[('id', 'in', available_location_ids)]"/>


الصورة الرمزية
إهمال