Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
14004 Lượt xem

hello

please ! I need your help I want to add a fields.function 'cost' in my object that returns the sum of two fields from an other object "vehicule". I tried with this code but it seems not working " ProgrammingError: can't adapt type 'dict' "

class location_location(osv.osv):

_name = 'location.location'
def get_total(self, cr, uid, ids, values, arg, context):
x={}
for record in self.browse(cr, uid, ids):
    x[record.id]= record.vehicule_id.tarif_id.prixjournalier + record.vehicule_id.tarif_id.prixhebdomadaire
    return x

_columns = {
    'name': fields.char('ID',size=32,required=True),
    'vehicule_id': fields.many2one('vehicule.vehicule','Véhicule',required=True),
'cost' : fields.function(get_total, method=True, string='Total',type='float'),
}

location_location()

class vehicule_vehicule(osv.osv):

_name = 'vehicule.vehicule'
_columns = {
    'name': fields.char('ID', size=32, required=True, help='L\'identifiant du véhicule '),
'modele_id': fields.many2one('vehicule.modele','Modéle'),
'tarif_id': fields.many2one('vehicule.tarif','Tarif de location'),
    }

vehicule_vehicule()

class vehicule_tarif(osv.osv):

_name = 'vehicule.tarif'
_columns = {
'name': fields.char('ID', size=32, required=True),
    'prixjournalier': fields.integer('Prix journalier'),
    'prixhebdomadaire': fields.integer('Prix hebdomadaire'),
}

vehicule_tarif()

the value calculated will appear in the tree as "cost" please what I can do ?

Ảnh đại diện
Huỷ bỏ

where is your vehicule model table?

Câu trả lời hay nhất
from osv import fields,osv
class vehicule_tarif(osv.osv):
    _name = 'vehicule.tarif'
    _columns = {
                'name': fields.char('ID', size=32, required=True),
                'prixjournalier': fields.integer('Prix journalier'),
                'prixhebdomadaire': fields.integer('Prix hebdomadaire'),
                }
vehicule_tarif()

class vehicule_vehicule(osv.osv):
    _name = 'vehicule.vehicule'
    _columns = {
        'name': fields.char('ID', size=32, required=True, help='L\'identifiant du véhicule '),
#    'modele_id': fields.many2one('vehicule.modele','Modéle'),
       'tarif_id': fields.many2one('vehicule.tarif','Tarif de location'),
        }
vehicule_vehicule()    

class location_location(osv.osv):
    _name = 'location.location'
    def get_total(self, cr, uid, ids, values, arg, context):
        x={}
        print "get_total"
        for record in self.browse(cr, uid, ids):
            x[record.id]= record.vehicule_id.tarif_id.prixjournalier + record.vehicule_id.tarif_id.prixhebdomadaire
        return x

    _columns = {
        'name': fields.char('Name',size=32,required=True),
        'vehicule_id': fields.many2one('vehicule.vehicule','Véhicule',required=True),
        'cost' : fields.function(get_total, method=True, string='Total',type='float'),
        }
location_location()  

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

and xml code for the above models

<openerp>
    <data>

    <!-- Test model View -->
    <menuitem id="menu_demo_sample" name="Demo Menu"
             sequence="3"/>
    <menuitem id="menu_demo_sub1" name="Locations" parent= "menu_demo_sample" sequence="1"/>

    <!-- Name : tarif : Tree View -->

    <record id="view_tarif_tree" model="ir.ui.view">
            <field name="name">vehicule.tarif</field>
            <field name="model">vehicule.tarif</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="Tarif">
                    <field name="name"/>

                </tree>
            </field>        
        </record>




        <!-- Name : tarif : Form View -->

        <record id="view_tarif_form" model="ir.ui.view">
            <field name="name">vehicule.tarif</field>
            <field name="model">vehicule.tarif</field>
            <field name="arch" type="xml">
                <form string="Tarif" version="7.0">
                    <group>         
                        <field name="name"/>
                        <field name="prixjournalier"/>
                        <field name="prixhebdomadaire"/>
                    </group>
                </form>
            </field>        
        </record>

        <!-- Name : vehicule : Tree View -->

    <record id="view_vehicule_tree" model="ir.ui.view">
            <field name="name">vehicule.vehicule</field>
            <field name="model">vehicule.vehicule</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="Vehicule">
                    <field name="name"/>


                </tree>
            </field>        
        </record>




        <!-- Name : vehicule : Form View -->

        <record id="view_vehicule_form" model="ir.ui.view">
            <field name="name">vehicule.vehicule</field>
            <field name="model">vehicule.vehicule</field>
            <field name="arch" type="xml">
                <form string="Vehicule" version="7.0">
                    <group>         
                        <field name="name"/>
                        <field name="tarif_id"/>
                    </group>
                </form>
            </field>        
        </record>

    <!-- Name : test : Tree View -->

        <record id="view_demo_tree" model="ir.ui.view">
            <field name="name">location.location</field>
            <field name="model">location.location</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="Location">
                    <field name="name"/>

                </tree>
            </field>        
        </record>




        <!-- Name : test : Form View -->

        <record id="view_demo_form" model="ir.ui.view">
            <field name="name">location.location</field>
            <field name="model">location.location</field>
            <field name="type">form</field>

            <field name="arch" type="xml">
                <form string="Location" version="7.0">
                    <group>             
                        <field name="name"/>
                        <field name="vehicule_id"/>
                        <field name="cost"/>
                    </group>    
                </form>
            </field>        
        </record>

        <!--Name : test Action : Action Menu  -->

        <record id="action_demo_menu" model="ir.actions.act_window">
            <field name="name">Demo Menu</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">location.location</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="view_id" ref="view_demo_tree"/>
        </record>


       <menuitem name="Sample Result" action="action_demo_menu" id="menu_demo_sub2" parent="menu_demo_sub1" sequence="0"/>
    </data>
</openerp>

after entering a value save the record thereby you can see the total price. thank you

Ảnh đại diện
Huỷ bỏ

hope the answer is okay mos

Tác giả

Thank you brother ! it works.

Câu trả lời hay nhất

If I am not wrong, you have kept the return x inside for loop. Get it out of the loop and see. Thanks.

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Thank you for your answer, the return is already out of the loop, I just made a mistake when I was typing the code here. Always the same problem :(

Please can you guive a simple exemple of the fields.function that illustrating how to use them. Thank you.

Ảnh đại diện
Huỷ bỏ