Can't you write it like this (untested)?

#----
class CRUDSession(sqlalchemy.orm.Session):
    def sacrud(self, cls):
        return CRUD(self, cls)

Session = scoped_session(sessionmaker(class_=CRUDSession))
#----

Simon

On Mon, Mar 30, 2015 at 3:15 PM, uralbash <svintso...@gmail.com> wrote:
> More example in tests
> https://github.com/ITCase/sacrud/blob/master/sacrud/tests/test_sessionmaker.py
>
>     def test_create(self):
>         user = self.session.sacrud(User)\
>             .create({'name': 'Dzhirad', 'fullname': 'Kiri', 'password':
> 123})
>         self.assertEqual(user.name, 'Dzhirad')
>         db_user = self.session.query(User).get(user.id)
>         self.assertEqual(user.id, db_user.id)
>
>     def test_read(self):
>         self._add_item(User, 'Vasya', 'Pupkin', "123")
>         user = self.session.sacrud(User).read(1)
>         self.assertEqual(user.name, 'Vasya')
>         self.assertEqual(user.id, 1)
>
>     def test_update(self):
>         self._add_item(User, 'Vasya', 'Pupkin', "123")
>         user = self.session.sacrud(User).update(1, {'name': 'Bill'})
>         db_user = self.session.query(User).get(user.id)
>         self.assertEqual(user.name, 'Bill')
>         self.assertEqual(user.id, db_user.id)
>
>     def test_delete(self):
>         user = self._add_item(User, 'Volod', 'Khonin', "123")
>         self.session.sacrud(User).delete(user.id)
>         db_user = self.session.query(User).filter_by(id=user.id).all()
>         self.assertEqual(db_user, [])
>
>
>
>
> понедельник, 30 марта 2015 г., 17:34:52 UTC+5 пользователь Simon King
> написал:
>>
>> On Mon, Mar 30, 2015 at 1:09 PM, uralbash <svint...@gmail.com> wrote:
>> > Hello,
>> >
>> > I want to set up in each session was my custom attribute, for example:
>> >
>> > from sqlalchemy.orm import scoped_session, sessionmaker
>> >
>> > Session = my_wapper(scoped_session(sessionmaker()))
>> > session1 = Session()
>> > session2 = Session()
>> >
>> > hasattr(session1, foo)  # True: What I want
>> > hasattr(session2, foo)  # True: What I want
>> > print(session1.foo)
>> >
>> > What is the best way to do it? I have no idea how I can change the
>> > object
>> > from __call__ function. I also did not find that something like
>> > Session.set_property.
>> >
>>
>> You can define a subclass of the sqlalchemy Session, and ask
>> sessionmaker to return instances of that class by passing the "class_"
>> parameter to sessionmaker():
>>
>>
>> http://docs.sqlalchemy.org/en/rel_0_9/orm/session_api.html#session-and-sessionmaker
>>
>> Would that do what you wanted?
>>
>> Simon
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to