Skip to Content
Menu
This question has been flagged
3 Replies
4902 Views

Hello,


I am struggling a bit with something and I am hoping that somebody here with a lot more experience can help me. 


I am creating a module where I have a few models, the relevant ones are: 

  • event
  • venue
  • hall
  • stand

This module is to manage stands in tradeshows, so an event can have a lot of stands, but only one venue, and a venue can have many halls and each hall can have many stands. 

At the moment it works, I am using Many2one fields to link everything up and it works. However, when I create a stand, I want to filter the options of the hall where the stand will be, to display only the available halls of the venue where the parent event is happening. 

I have been trying to accomplish that filter by using this in the stand model:

  ​event_id = fields.Many2one('dev.event', string='Event')
hall_id = fields.Many2one('dev.hall', string='Hall',
​domain="[('venue_id','=','event_id.venue_id')]")

Although the code doesnt throw an error, it also doesnt show any results.


I also tried taking the aproach of using a function for the domain... but that fell beyond my current experience, so I couldnt make it work.


Any help will be greatly appreciated.



Avatar
Discard
Author Best Answer

Hello


Mily Shajan


Thank you very much for the links. I followed the video, but when I got to download the web_domain_field module, it says it is deprecated, therefore I will leave it as a last resort.


Cybrosys


Thank you for the snippet. unfortuanately for some reason the domain does not get applied on return. I added a sneaky print() statement to verify that the function was firing and it is, but the domain remains unchanged in the field. 


Also, the isue of using the on_change hook is that it will only fire when the field is actually changed, so when I edit the record without changing the event it will display the unfiltered options. I have figured that if I create another field to display the name of the venue from a computed field, the function will fire when event is modified and also when the record is loaded. However, although the function does fire, the domain still doesnt get applied on return. Could it have some sort of typo or something?


@api.depends('event_id')

def compute_hall_ids(self):

        domain = []

        for rec in self:

            print(f'The venue Id of the event {rec.event_id.name} is: {rec.event_id.venue_id.id}')

            rec.venue_name = rec.event_id.venue_id.name

            domain = [('venue_id.id', '=', rec.event_id.venue_id.id)]

        print(domain)

        return {'domain': {'hall_id': domain}}


The printout I get for the domain is  [('venue_id', '=', 7)] as an example



cheers!


FINAL SOLUTION:

After reading the documentation more in depth, I found out that the @api.onchange and @api.depends decorators, will not fire the return statement, so that is why it wasnt being applied. 

Using the web_domain_field module did the trick for me. Even on V17. 

Cheers!



Avatar
Discard

Hi!
I am dealing with the same issue, working with odoo 17.

So to summarize, the only way you could solve it was by intslaling the Web Domain Field module? (https://apps.odoo.com/apps/modules/12.0/web_domain_field)

Thank you,
Nicolás

Best Answer

Hi,

You can solve the issue by applying the domain dynamically.

    


@api.onchange('event_id')

def _onchange_event_id(self):

   if self.event_id:

       domain = [('venue_id', '=', self.event_id.venue_id.id)]

   else:

       domain = []

   return {'domain': {'hall_id': domain}}


You can also refer to How to Apply Dynamic Domain for Relational Fields in 


Hope it helps

Avatar
Discard
Best Answer

Hi 

Create a dynamic domain for the field. Check this reference to set a dynamic domain for the field

Dynamic Domain for a field

Domain for Relational Fields in Odoo


Regards

Avatar
Discard
Related Posts Replies Views Activity
2
Feb 24
1572
3
May 25
3161
4
May 25
1263
2
May 25
4418
0
May 25
442