[sqlalchemy] Re: no error when using wrong 'attribute' name on an object
Michael Bayer a écrit : well, theres nothing that says the attribute youre sending in is wrong. classes in python dont have any notion of predeclared attribute names. a mapped class can have any number of other attributes which dont correspond to database-mapped attributes. Yes, of course. I was focused on the database... in your case, you would like to constrain the attributes on your class to the set of those which have been explicitly set up in a mapping relationship. youd implement your own constructor like this: class MyBaseClass(ActiveMapper): def __init__(self, **kwargs): mapper = class_mapper(self.__class__) for key, value in kwargs.items(): if key not in mapper.props: raise AttributeError(non mapped attribute: '%s' % key) setattr(self, key, value) class Color(MyBaseClass): # etc I'll try this approach. Thanks. while im not a big ActiveMapper user, id leave it up to the ActiveMapper developers if they think this behavior should be built in to ActiveMapper itself (i kind of dont think it should be). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: no error when using wrong 'attribute' name on an object
well, theres nothing that says the attribute youre sending in is wrong. classes in python dont have any notion of predeclared attribute names. a mapped class can have any number of other attributes which dont correspond to database-mapped attributes. in your case, you would like to constrain the attributes on your class to the set of those which have been explicitly set up in a mapping relationship. youd implement your own constructor like this: class MyBaseClass(ActiveMapper): def __init__(self, **kwargs): mapper = class_mapper(self.__class__) for key, value in kwargs.items(): if key not in mapper.props: raise AttributeError(non mapped attribute: '%s' % key) setattr(self, key, value) class Color(MyBaseClass): # etc while im not a big ActiveMapper user, id leave it up to the ActiveMapper developers if they think this behavior should be built in to ActiveMapper itself (i kind of dont think it should be). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---