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

Best practices as to when to use fields.selection vs fields.many2one

By
john5000
on 12/1/14, 1:54 PM 942 views

I've seen a model that has a status or state field which is done as fields.selection() and I've seen it done as a fields.many2one().  Both ways of doing it seem to work fine and do the job. 

I suppose many2one is best when you expect to have new values to add over time. 

I am wondering if there are any best practices as to which should be used under what cirmustances.  And what are the advantages and disavantages of each.  Or any notable experince in using one or the other.

 

Hello Best practice according to me is to use : many2one : for relations with another object (wich can be edited... add choice, ...) Eg: Category, manager, ... Selection : A static list (not evolutive). Eg: Status (Open / WIP / Close) JKE

Jérémy Kersten (jke)
on 12/1/14, 3:06 PM

Thank you jke, that is simple and makes sense.

john5000
on 12/2/14, 5:27 PM
1

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 12/1/14, 7:52 PM

I wouldn't say that this is best practices, but here are some "differences" that would be your consideration points when making the design decision:

  • selection is a fixed list once rendered (this also applies to many2one fields which is viewed with widget="selection"), you cannot apply domain to it
  • selection will be pulled altogether at once when HTML is rendered (this also applies to many2one fields which is viewed with widget="selection").  Not a good choice if you have hundreds or thousands selection.
  • selection fields (this does NOT apply to many2one fields viewed with widget="selection") is not stored as a Foreign Key.  So you can remove an entry from the selection and no integrity problem will ensue.

As for the states, you can still make it a selection field whilst making it dynamic by supplying the entries from a table.  Selection field can accept a function as selection source.

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

2 follower(s)

Stats

Asked: 12/1/14, 1:54 PM
Seen: 942 times
Last updated: 3/16/15, 8:10 AM