| | 140 | |
| | 141 | There is an alternate form of the ``has_many`` relationship that takes only |
| | 142 | two keyword arguments: `through` and `via` in order to encourage a more |
| | 143 | rich form of many-to-many relationship that is an alternative to the |
| | 144 | ``has_and_belongs_to_many`` statement. Here is an example: |
| | 145 | |
| | 146 | :: |
| | 147 | |
| | 148 | class Person(Entity): |
| | 149 | has_field('name', Unicode) |
| | 150 | has_many('assignments', of_kind='Assignment', inverse='person') |
| | 151 | has_many('projects', through='assignments', via='project') |
| | 152 | |
| | 153 | class Project(Entity): |
| | 154 | has_field('title', Unicode) |
| | 155 | has_many('assignments', of_kind='Assignment', inverse='project') |
| | 156 | |
| | 157 | class Assignment(Entity): |
| | 158 | has_field('start_date', DateTime) |
| | 159 | belongs_to('person', of_kind='Person', inverse='assignments') |
| | 160 | belongs_to('project', of_kind='Project', inverse='assignments') |
| | 161 | |
| | 162 | In the above example, a `Person` has many `projects` through the `Assignment` |
| | 163 | relationship object, via a `project` attribute. |
| | 526 | def __init__(self, entity, name, *args, **kwargs): |
| | 527 | if 'through' in kwargs and 'via' in kwargs: |
| | 528 | setattr(entity, name, association_proxy(kwargs.get('through'), kwargs.get('via'))) |
| | 529 | return |
| | 530 | |
| | 531 | super(HasMany, self).__init__(entity, name, *args, **kwargs) |
| | 532 | |