コンテンツへスキップ
メニュー
この質問にフラグが付けられました
8 返信
12322 ビュー

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

アバター
破棄

Hello yenthe,

I am also facing the same issue in odoo 12. So did you find any solution for that?

最善の回答

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.

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

Hi @Yenthe, can you tell me how you do this?

最善の回答

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: 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.

関連投稿 返信 ビュー 活動
0
7月 25
1
2
7月 25
270
0
7月 25
3
0
7月 25
248
1
7月 25
356