This question has been flagged
2 Replies
11465 Views

Hello,

I want to change the view of the "Contacts/new" form when the "citizen" (I added it in my .py file as a boolean field) is ticked. When it is True, I want the "zip" field to be read-only type with default value "12345" , and when it is False just normally.

If I was not completely clear, or if you need my code, please let me know.

Avatar
Discard
Author Best Answer
Thanks for the advice @Ibrahim ,
I added this in my code

my .py
citizens = fields.Boolean('Citizens', default=True)

@api.onchange('citizens')
def _is_citizens(self):
if self.citizens:
    self.zip = 85320
else:
    self.zip = None

my .xml
<field name="zip" position="replace">
<field
name="zip" placeholder="Zip" attrs="{'readonly':[('citizens','=',True)]}"/>
</field>


now it works as I wanted
Avatar
Discard
Best Answer

Hi,

If i were you, i would do this : 

1- Create a second menu called Citizens. 
2- in the action, i will add a domain [('is_citizen', '=', True)] and a context {'default_citizen': True, 'default_zip':"12345"}
3- In form view, boolean citizen will be invisible, zip will be readonly. I would think of adding a tag called "Citizen" by default.
4- in Contacts, i will add domain [('is_citizen':False)]

That is to say, Citizens will be completely separated from other Contacts.

What do you think ? 
Regards.

Avatar
Discard