Just sqlalchemy filter not working

Select everything works as follows:

q = session.query(products)

Now I want to add a WHERE filter, so I'm trying:

q = session.query(products).filter_by(stock_count=0)

I get an error in which the nonetype object does not have the class_manager attribute.

Not sure what the problem is?

Update The column seems to display correctly, how and when:

q = session.query(products)

for p in q:
   print p.stock_count

It displays the value.

But if I do this:

p.stock_count = 6

I also get an error: "Unable to set attribute"

So I can query it, but adding a column as a filter, or setting the value causes an error.

Strange no?

+3
source share
4 answers

, orm " ".

0,5 (, 6.2): ​​

#!/usr/bin/env python
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

db = create_engine(localopts.connect_string)
Session = sessionmaker(bind=db)

Base = declarative_base()
Base.metadata.reflect(bind=db)

class some_table(Base): 
    __table__ = Base.metadata.tables['table_name']

session = Session()

for row in session.query(some_table.username).filter_by(username="some-user"):
    print row
+2

.all() filter_by:

q = session.query(products).filter_by(stock_count=0).all()
0

Literal Sql? , sql, .

, - :

q = session.query(products).filter('stock_count==0')
0

filter_by() , filter(). , stock_count (, ), products.stock_count , , products.__class__.stock_count. : q=session.query(products).filter(product.stock_count==0)

0

Source: https://habr.com/ru/post/1757746/


All Articles