Changeset 355
- Timestamp:
- 07/07/08 14:55:41 (5 years ago)
- Location:
- elixir/trunk
- Files:
-
- 3 modified
-
CHANGES (modified) (1 diff)
-
elixir/relationships.py (modified) (4 diffs)
-
tests/test_o2m.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
elixir/trunk/CHANGES
r354 r355 22 22 any). 23 23 - Added on_reconstitute event/method decorator. Only works with SA 0.5. 24 - Added support for viewonly relationships (OneToMany and OneToOne). 24 25 25 26 Changes: -
elixir/trunk/elixir/relationships.py
r350 r355 413 413 414 414 kwargs.update(self.get_prop_kwargs()) 415 415 416 self.property = relation(self.target, **kwargs) 416 417 self.add_mapper_property(self.name, self.property) … … 451 452 452 453 def is_inverse(self, other): 453 return other is not self and \ 454 # viewonly relationships shouldn't match as inverse of anything (so 455 # that no backref is created -- which doesn't make sense in that case) 456 viewonly = self.kwargs.get('viewonly', False) or \ 457 other.kwargs.get('viewonly', False) 458 return not viewonly and \ 459 other is not self and \ 454 460 self.match_type_of(other) and \ 455 461 self.entity == other.target and \ … … 612 618 613 619 def create_keys(self, pk): 620 # When using a viewonly relationship, you are on your own: Elixir 621 # doesn't check that a corresponding ManyToOne relationship exists. 622 if self.kwargs.get('viewonly', False): 623 return 624 614 625 # make sure an inverse relationship exists 615 626 if self.inverse is None: … … 637 648 kwargs['remote_side'] = self.inverse.foreign_key 638 649 639 if self.inverse.primaryjoin_clauses: 640 kwargs['primaryjoin'] = and_(*self.inverse.primaryjoin_clauses) 650 # viewonly relationships do not have any inverse (and they provide 651 # their primaryjoin argument manually anyway). 652 if not self.kwargs.get('viewonly', False): 653 if self.inverse.primaryjoin_clauses: 654 kwargs['primaryjoin'] = and_(*self.inverse.primaryjoin_clauses) 641 655 642 656 kwargs.update(self.kwargs) -
elixir/trunk/tests/test_o2m.py
r349 r355 4 4 5 5 from elixir import * 6 from sqlalchemy import and_ 7 from sqlalchemy.ext.orderinglist import ordering_list 6 8 7 9 def setup(): … … 105 107 print root 106 108 109 def test_viewonly(self): 110 class User(Entity): 111 two_blurbs = OneToMany('Blurb', primaryjoin=lambda: 112 and_(Blurb.user_id == User.id, Blurb.position < 2), 113 viewonly=True 114 ) 115 blurbs = OneToMany('Blurb', 116 collection_class=ordering_list('position'), 117 order_by='position') 118 119 class Blurb(Entity): 120 user = ManyToOne('User') 121 position = Field(Integer) 122 blurb = Field(Unicode(255)) 123 124 def __init__(self, blurb, **kwargs): 125 super(Blurb, self).__init__(blurb=blurb, **kwargs) 126 127 def __repr__(self): 128 return 'Blurb(%r, %r)' % (self.position, self.blurb) 129 130 setup_all(True) 131 132 user = User(blurbs=[Blurb(u'zero'), Blurb(u'one'), Blurb(u'two')]) 133 134 session.commit() 135 session.clear() 136 137 user = User.get(1) 138 assert len(user.two_blurbs) == 2 139 assert user.two_blurbs[0].blurb == 'zero' 140 assert user.two_blurbs[1].blurb == 'one' 141 107 142 def test_has_many_syntax(self): 108 143 class Person(Entity):
