Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to define custom settings on configuration view?

By
Yenthe
on 8/21/15, 12:06 PM 1,733 views

Hi guys,

I've created a new view which should always only allow one record, so I've made a view just like under Settings > Configuration.
1) Create a menu item

 <menuitem id="menu_tech_settings_screen"
                action="technical_details_odoo_installer_action" parent="server_main_menu"
                sequence="30"/>

2) Add an act_window behind it:

 <record id="technical_details_odoo_installer_action" model="ir.actions.act_window">
            <field name="name">Configure settings</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">odoo.environment.technical.settings</field>
            <field name="view_id" ref="configure_technical_settings_odoo_installer"/>
            <field name="view_mode">form</field>
            <field name="target">inline</field>
        </record>

3) Create a view just like under Settings > Configuration > General Settings:

 <record id="configure_technical_settings_odoo_installer" model="ir.ui.view">
            <field name="name">Technical configuration</field>
            <field name="model">odoo.environment.technical.settings</field>
            <field name="arch" type="xml">
                <form string="Configure Sales" class="oe_form_configuration">
                    <header>
                        <button string="Apply" type="object" name="execute" class="oe_highlight"/>
                        or
                        <button string="Cancel" type="object" name="cancel" class="oe_link"/>
                    </header>
		<div name="configuration">
		    <group>
		        <field name="my_custom_field"/>
		    </group>
		</div>
		</form>
	    </field>
	</record>

The view is being rendered fine but I'm now wondering how to proceed. How exactly can I store the new value when a user clicks on the Apply button? There should only be one record at all times and this is used to configure the rest on. I've inherited res.config.settings, created my own model and I have a get and set function for this. Example:

 class my_custom_class(models.Model):
    _name = 'my.custom.model'
    _inherit = 'res.config.settings'
    odoo_username = fields.Char('Ubuntu users password',help='Please fill in the password for the Ubuntu user that runs this Odoo. This password is needed to automatically install a new Odoo server through a shell script. Installing a new Odoo needs sudo rights!',required=True)

    def get_default_username(self, cr, uid, fields, context=None):
        odoo_username=self.pool.get('my.custom.model').browse(cr, uid, uid, context=context).odoo_username
        return {'odoo_username': odoo_username}

    def set_default_username(self, cr, uid, ids, context=None):
        config = self.browse(cr, uid, ids[0], context)
        new_odoo_username=config.odoo_username
        self.pool.get('my.custom.model').write(cr, uid, uid, {'odoo_username': new_odoo_username})

The problem here is that this will still create a new record every time and I'm not sure if this is the correct behaviour for this type of view either.

Thanks,
Yenthe

1

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
896
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

Tarek Mohamed Ibrahim
On 8/28/15, 6:06 PM

I checked your code, but since I don't have much experience with inheriting res.config.settings I can't judge if this is not appropriate, or find a bug in it. 

Instead, I came up with a different solution, you don't need to inherit the settings class, you can create a new class for your setup purpose.


Code is below:

This is the class:

class my_custom_class(osv.osv):
    _name = 'my.custom.class'
    _description = 'my_custom_class'
    _columns = {
        'odoo_username':fields.char(string='odoo_username',),
        }
my_custom_class()

 

This is the view, along with the menu and the action

<record id="my_custom_class_view" model="ir.ui.view">
<field name="name">my.custom.class.view</field>
<field name="model">my.custom.class</field>
<field name="arch" type="xml">
<form string="my custom class" create="false" delete="false">
<field name="odoo_username" string="Odoo Username"/>
</form>
</field>
</record>
<record id="act_my_custom_class_view" model="ir.actions.act_window">
<field name="name">my.custom.class.view</field>
<field name="res_model">my.custom.class</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
</record>
<!-- the following is a one record as initial data -->
<record id="my_custom_class_only_one_record" model="my.custom.class">
<field name="odoo_username">"test odoo user name"</field>
</record>

<menuitem action="act_my_custom_class_view" id="menu_act_my_custom_class_view" parent="some_module.some_parent_id" sequence="30"/>

Note that I tagged the view form with 

create="false" delete="false" 

so, no one can add or delete records to this view

I also furnished this class with one data record with initial value(s) user can starts with.

I hope this will help until we get some more experience with the res.config.settings, which is the standard for this purpose.

Thanks for your answer Tarek! I gave you a +1 since its another option and it would work but I honestly do not think this is the ideal way to do it.

Yenthe
on 9/1/15, 4:27 AM

I agree with you that this is not the ideal way, and for that I said that "I hope this will help until we get some more experience with the res.config.settings". This is a workaround, I do that when I'm pressed in time and need to deliver to the customer. When got the idea and there is an area to enhance, I provide the customer with the standard solution

Tarek Mohamed Ibrahim
on 9/1/15, 4:37 AM
0

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 9/1/15, 10:32 AM

HI Yenthe,

These solution might helps you.

1) Override create method of new model and search all the records created with new model and check length if  already having 1 than either raise warning or just return false instead of retuning result of super. this will stop user to creating more than 1 record in new model.

2) Apply create="false" and edit="false" in both tree and form view of new model. and create of actions todo with trigger your actions in that case open the form view in edit mode which will trigger once you install or upgrade module after adding this feature which will create record as you want than . after that user will not able to add any record inside as create & edit is false already.

Hope this might help.

Rgds,

Anil.




Thanks for your answer Anil! While this is a way and it will work I can't imagine that this is the way to go. Sounds like a lot of hassle and code for such a little functionality.

Yenthe
on 9/1/15, 10:36 AM

@yenthe: If you don't want to show that record that user than don't create any action or view for that. Just create record in back end by automated actions as simple as that. But if you want to show that records in UI with different view and model than of course you have to create views and action for it.

Anil R. Kesariya
on 9/1/15, 1:02 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 8/21/15, 12:06 PM
Seen: 1733 times
Last updated: 9/1/15, 10:32 AM