On Mon, Apr 26, 2010 at 8:24 AM, Michael Bayer <mike...@zzzcomputing.com> wrote:
> Torsten Landschoff wrote:
>>
>> Thanks for your reply and the remarks! Do you plan to extend
>> attribute_mapped_collection to update the key like in my example?
>
> here's some things to note on that:
>
> 1. I'm not sure why it wasn't that way already, and I'd want to hear from
> Jason Kirtland, its author, on if we are missing something or otherwise
> whats up.   I have a vague notion that there was a reason for this, or
> maybe not.

It's not something that can be easily solved in the general case with
the current API. The mapped collections use a 'keyfunc' to figure out
the dictionary key for loaded instances, for example
'operator.attrgetter("name")' for attribute_mapped_collection("name").
 Mechanically reversing that logic in a setting operation sounds
pretty hard to me, but perhaps if we allowed an 'assignfunc' function
to be supplied that would do the trick.  Internally, the collection
code would call it during a "dict['key'] = instance" assignment
operation, maybe passing just the "key" value and the instance:

  def assignfunc(key, instance):
      instance.name = key

For highly constrained types like the attribute- and column-mapped
collections, these functions would be easy to generate.

A good test for the feature would be a mapped collection that maps a
tuple of attributes, such as one created by
"attribute_mapped_collection(('x', 'y'))".  Assigning "collection[1,
2] = instance" should assign both instance.x and instance.y in that
case.

> 2. I wonder if there's a way to make this happen more deeply than within
> setattr().   Like the collection internals would include an event to
> operate upon the target object that includes the other args from the
> collection decorator.

I have a hunch this is only meaningful for mapped collections-
mutations like list_collection[2:5] would be difficult to translate
and I'm not sure what information one would want to capture there.
Worth a look though.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to