1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
from odoo import api, fields, models, _
from odoo.osv.expression import FALSE_DOMAIN
def selection_fn(self):
return [(str(key), val) for key, val in enumerate([_("Corge"), _("Grault"), _("Wheee"), _("Moog")])]
def compute_fn(records):
for record in records:
record.value = 3
def inverse_fn(records):
pass
MODELS = [
('boolean', fields.Boolean()),
('integer', fields.Integer(default=4)),
('float', fields.Float()),
('decimal', fields.Float(digits=(16, 3))),
('string.bounded', fields.Char(size=16)),
('string.required', fields.Char(size=None, required=True)),
('string', fields.Char(size=None)),
('date', fields.Date()),
('datetime', fields.Datetime()),
('text', fields.Text()),
('selection', fields.Selection([('1', "Foo"), ('2', "Bar"), ('3', "Qux"), ('4', '')])),
('selection.function', fields.Selection(selection_fn)),
# just relate to an integer
('many2one', fields.Many2one('export.integer')),
('one2many', fields.One2many('export.one2many.child', 'parent_id')),
('many2many', fields.Many2many('export.many2many.other')),
('function', fields.Integer(compute=compute_fn, inverse=inverse_fn)),
# related: specialization of fields.function, should work the same way
('reference', fields.Reference([('export.integer', 'integer')], 'export.reference')),
]
for name, field in MODELS:
class NewModel(models.Model):
_name = f'export.{name}'
_description = f'Export: {name}'
_rec_name = 'value'
const = fields.Integer(default=4)
value = field
def _compute_display_name(self):
for record in self:
record.display_name = f"{self._name}:{record.value}"
@api.model
def _search_display_name(self, operator, value):
if isinstance(value, str) and value.split(':')[0] == self._name:
return [('value', operator, int(value.split(':')[1]))]
return FALSE_DOMAIN
class One2ManyChild(models.Model):
_name = 'export.one2many.child'
_description = 'Export One to Many Child'
# FIXME: orm.py:1161, fix to display_name on m2o field
_rec_name = 'value'
parent_id = fields.Many2one('export.one2many')
str = fields.Char()
m2o = fields.Many2one('export.integer')
value = fields.Integer()
def _compute_display_name(self):
for record in self:
record.display_name = f"{self._name}:{record.value}"
@api.model
def _search_display_name(self, operator, value):
if isinstance(value, str) and value.split(':')[0] == self._name:
return [('value', operator, int(value.split(':')[1]))]
return FALSE_DOMAIN
class One2ManyMultiple(models.Model):
_name = 'export.one2many.multiple'
_description = 'Export One To Many Multiple'
_rec_name = 'parent_id'
parent_id = fields.Many2one('export.one2many.recursive')
const = fields.Integer(default=36)
child1 = fields.One2many('export.one2many.child.1', 'parent_id')
child2 = fields.One2many('export.one2many.child.2', 'parent_id')
class One2ManyChildMultiple(models.Model):
_name = 'export.one2many.multiple.child'
# FIXME: orm.py:1161, fix to display_name on m2o field
_rec_name = 'value'
_description = 'Export One To Many Multiple Child'
parent_id = fields.Many2one('export.one2many.multiple')
str = fields.Char()
value = fields.Integer()
def _compute_display_name(self):
for record in self:
record.display_name = f"{self._name}:{record.value}"
class One2ManyChild1(models.Model):
_name = 'export.one2many.child.1'
_inherit = 'export.one2many.multiple.child'
_description = 'Export One to Many Child 1'
class One2ManyChild2(models.Model):
_name = 'export.one2many.child.2'
_inherit = 'export.one2many.multiple.child'
_description = 'Export One To Many Child 2'
class Many2ManyChild(models.Model):
_name = 'export.many2many.other'
_description = 'Export Many to Many Other'
# FIXME: orm.py:1161, fix to display_name on m2o field
_rec_name = 'value'
str = fields.Char()
value = fields.Integer()
def _compute_display_name(self):
for record in self:
record.display_name = f"{self._name}:{record.value}"
@api.model
def _search_display_name(self, operator, value):
if isinstance(value, str) and value.split(':')[0] == self._name:
return [('value', operator, int(value.split(':')[1]))]
return FALSE_DOMAIN
class SelectionWithDefault(models.Model):
_name = 'export.selection.withdefault'
_description = 'Export Selection With Default'
const = fields.Integer(default=4)
value = fields.Selection([('1', "Foo"), ('2', "Bar")], default='2')
class RecO2M(models.Model):
_name = 'export.one2many.recursive'
_description = 'Export One To Many Recursive'
_rec_name = 'value'
value = fields.Integer()
child = fields.One2many('export.one2many.multiple', 'parent_id')
class OnlyOne(models.Model):
_name = 'export.unique'
_description = 'Export Unique'
value = fields.Integer()
value2 = fields.Integer()
value3 = fields.Integer()
_sql_constraints = [
('value_unique', 'unique (value)', "The value must be unique"),
('pair_unique', 'unique (value2, value3)', "The values must be unique"),
]
class InheritsParent(models.Model):
_name = _description = 'export.inherits.parent'
value_parent = fields.Integer()
class InheritsChild(models.Model):
_name = _description = 'export.inherits.child'
_inherits = {'export.inherits.parent': 'parent_id'}
parent_id = fields.Many2one('export.inherits.parent', required=True, ondelete='cascade')
value = fields.Integer()
class Many2String(models.Model):
_name = _description = 'export.m2o.str'
child_id = fields.Many2one('export.m2o.str.child')
class ChidToString(models.Model):
_name = _description = 'export.m2o.str.child'
name = fields.Char()
class WithRequiredField(models.Model):
_name = _description = 'export.with.required.field'
name = fields.Char()
value = fields.Integer(required=True)
class Many2OneRequiredSubfield(models.Model):
_name = _description = 'export.many2one.required.subfield'
name = fields.Many2one('export.with.required.field')
|