se adiciona fields.One2Many
FossilOrigin-Name: 94c1cca50451a46c417d925b27fdd53d8199b8dc58783e600c84179eac666a36
This commit is contained in:
parent
ab462a6ca5
commit
5f5a6182c9
@ -1,10 +1,11 @@
|
||||
from .attribute import Attribute
|
||||
from .many2one import Many2One
|
||||
from .one2many import One2Many
|
||||
from .function import Function
|
||||
from .virtual import Virtual
|
||||
from .field import Field
|
||||
|
||||
__all__ = [Attribute, Many2One, Virtual, Field]
|
||||
__all__ = [Attribute, One2Many, Many2One, Virtual, Field]
|
||||
|
||||
def on_change(fields):
|
||||
from functools import wraps
|
||||
|
@ -26,7 +26,7 @@ class Field:
|
||||
if callable(call):
|
||||
return call(*args)
|
||||
|
||||
def _create_model(self, inst, name=None, model=None):
|
||||
def _create_model(self, inst, name=None, model=None, attribute=None):
|
||||
try:
|
||||
return inst._fields[self.name]
|
||||
except KeyError:
|
||||
@ -37,7 +37,12 @@ class Field:
|
||||
if name is not None:
|
||||
obj.__name__ = name
|
||||
self._set_namespace(obj, self.namespace, inst.__namespace__)
|
||||
inst._fields[self.name] = obj
|
||||
|
||||
if attribute:
|
||||
inst._fields[attribute] = obj
|
||||
else:
|
||||
inst._fields[self.name] = obj
|
||||
|
||||
return obj
|
||||
|
||||
def _changed_field(self, inst, name, value):
|
||||
|
25
facho/model/fields/one2many.py
Normal file
25
facho/model/fields/one2many.py
Normal file
@ -0,0 +1,25 @@
|
||||
from .field import Field
|
||||
|
||||
class BoundModel:
|
||||
def __init__(self, creator):
|
||||
self.creator = creator
|
||||
|
||||
def create(self):
|
||||
return self.creator()
|
||||
|
||||
class One2Many(Field):
|
||||
def __init__(self, model, name=None, namespace=None, default=None):
|
||||
self.model = model
|
||||
self.field_name = name
|
||||
self.namespace = namespace
|
||||
self.default = default
|
||||
self.count_relations = 0
|
||||
|
||||
def __get__(self, inst, cls):
|
||||
assert self.name is not None
|
||||
def creator():
|
||||
attribute = '%s_%d' % (self.name, self.count_relations)
|
||||
self.count_relations += 1
|
||||
return self._create_model(inst, name=self.field_name, model=self.model, attribute=attribute)
|
||||
|
||||
return BoundModel(creator)
|
@ -399,3 +399,20 @@ def test_model_on_change_field_attribute():
|
||||
person.hash = 'hola'
|
||||
assert '<Person react="hola+4" Hash="hola"/>' == person.to_xml()
|
||||
|
||||
def test_model_one2many():
|
||||
class Line(facho.model.Model):
|
||||
__name__ = 'Line'
|
||||
|
||||
quantity = fields.Attribute('quantity')
|
||||
|
||||
class Invoice(facho.model.Model):
|
||||
__name__ = 'Invoice'
|
||||
|
||||
lines = fields.One2Many(Line)
|
||||
|
||||
invoice = Invoice()
|
||||
line = invoice.lines.create()
|
||||
line.quantity = 3
|
||||
line = invoice.lines.create()
|
||||
line.quantity = 5
|
||||
assert '<Invoice><Line quantity="3"/><Line quantity="5"/></Invoice>' == invoice.to_xml()
|
||||
|
Loading…
Reference in New Issue
Block a user