Pregunta

can anyone explain the working of create and write orm mehods in openerp ? Actually I'm stuck at this methods,I'm not getting how it works internally and how can I implement it over a simple program.

class dumval(osv.osv):
    _name = 'dum_val'

    _columns={
           'state':fields.selection([('done','confirm'),('cancel','cancelled')],'position',readonly=True),
           'name':fields.char('Name',size=40,required=True,states={'done':[('required','False')]}),
           'lname':fields.char('Last name',size=40,required=True),
           'fname':fields.char('Full name',size=80,readonly=True),
           'addr':fields.char('Address',size=40,required=True,help='enter address'),
    }
    _defaults = {
              'state':'done',                  
    }

It would be nice if u could explain using this example..

¿Fue útil?

Solución

A couple of comments plus a bit more detail.

  1. As Lukasz answered, convention is to use periods in your model names dum.val. Usually something like my_module.my_model to ensure there are no name collisions (e.g. account.invoice, sale.order)

  2. I am not sure if your conditional "required" in the model will work; this kind of thing is usually done in the view but it would be worth seeing how the field is defined in the SQL schema.

The create method creates new records (SQL Insert). It takes a dict of values, applies any defaults you have specified and then inserts the record and returns the new ID. Note that you can do compound creates, i.e. if you are creating and invoice, you can add the invoice lines into the dictionary and do it all in one create and OpenERP will take care of the related fields for you (ref write method in https://doc.openerp.com/trunk/server/api_models/)

The write method updates existing records (SQL Update). It takes a dict of values and applies to all of the ids you pass. This is an important point, if you pass a list of ids, the values will be written to all ids. If you want to update a single record, pass a list of one entry, if you want to do different updates to the records, you have to do multiple write calls. You can also manage related fields with a write.

Otros consejos

It's convention to give _name like dum.val instead of dum_val. In dumval class you can write a method:

def abc(cr, uid, ids, context=None):
    create_dict = {'name':'xxx','lname':'xxx','fname':'xxx','addr':'xyz'}
    # create new object and get id
    new_id = self.create(cr, uid, write_dict, context=context)
    # write on new object
    self.write(cr, uid, new_id, {'lname':'yyy'}, context=context)

For more details look: https://www.openerp.com/files/memento/older_versions/OpenERP_Technical_Memento_v0.6.1.pdf

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top