Function field to emulate a one2many ask me to save unchanged record

Hello everyone,
I am trying to emulate a One2Many field. The idea is to set a list of parties and to show if meets some condition with another field.

At the time I have done 2 classes. The first one has the One2Many field function, and the second class is a ModelView class only.

The code is something like this (is not the same code, but it has the same behaviour):

   class Contact(ModelSQL,ModelView):
    (......)
    party = fields.Many2One('Party')
    contacts = fields.Function(
                   fields.One2Many('party.contact',None,'Contacts'), 
                   'get_contacts')
     
   def get_contacts(self,name):
       res = [ ]
       res.append(
                 {
                   'party': self.party.id,
                    'status': 'unreached',
                  })
        return (res)

   class  PartyContact(ModelView):
       ' Party Contact ' 
       __name__ = 'party.contact'
       party = fields.Many2One('party.party','Party')
       status = fields.Char('Status)

Each time I change the view or pass from one record to another or close the tab, tryton ask me to save the record.

Any help on that?
Thank you very much. Regards
Francisco

You can not emulate with a Function fields a One2Many because the client will create new record for each dictionary and so it will always try to save them.
You must emulate a Many2Many by returning real ids. This is a little bit more work but the result is more powerful.
Or maybe the party.contact could even be a table query and so you will have an actual One2Many.

1 Like

Yes! I notice on the empties One2Many.

I did it on another project. Maybe I will have to use more than one to have separate list for each condition. This would be the solution.

I don’t know whats this means.

Thank you very much for the quick and fast answer.
Francisco

A table query is a ModelSQL for which records are not stored in a table but retrieved from a SQL query. You can see that a little bit like a SQL view.

1 Like

Right! Maybe I will give a try on the next update. Thanks!