Jak uzyskać nazwy kolumn z wyniku SQLAlchemy (składnia deklaratywna)
Pracuję w projekcie piramidy i mam tabelę w SQLAlchemy w składni deklaratywnej
"""models.py"""
class Projects(Base):
__tablename__ = 'projects'
__table_args__ = {'autoload': True}
Otrzymuję wyniki używając
""""views.py"""
session = DBSession()
row_data = session.query(Projects).filter_by(id=1).one()
Jak mogę uzyskać nazwy kolumn z tego wyniku.
PS: nie jestem w stanie użyć tej metody, ponieważ używam składni deklaratywnej.
7 answers
Różnica jest między ORM i nie-ORM, nie deklaratywny, który jest tylko pomocnikiem dla ORM.
Zapytanie posiada metodę column_descriptions()
, która została dodana w tym celu::
Http://www.sqlalchemy.org/docs/orm/query.html#sqlalchemy.orm.query.Query.column_descriptions
Przykład wydaje się, że ma literówkę, mówi q.columns
ale powinna być q.column_descriptions
(edit: just fixed it).
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-17 09:06:43
Możesz zrobić coś podobnego do odpowiedzi stosu Foo bez uciekania się do prywatnych pól, wykonując:
conn.execute(query).keys()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-30 21:45:51
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import (Column, Index, Date, DateTime, Numeric, BigInteger, String, ForeignKey, Boolean)
Base = declarative_base()
class Project(Base):
"""sqlalchemy ORM for my table."""
__tablename__ = "table1"
id = Column("id", BigIntegerID, primary_key=True, autoincrement=True)
date = Column("date", Date, nullable=False)
value = Column("value", Numeric(20, 8))
...
...
Wtedy zwróci nazwy kolumn ['id', 'date', 'value',...]:
Project.__table__.columns.keys()
Lub to
Project.metadata.tables['table1'].columns.keys()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-04 20:42:54
Tylko się bawię, ta składnia da Ci wszystkie kolumny (więc aby rozwiązać problem, ustaw zapytanie tak, aby wyglądało tylko na jedną tabelę/obiekt):
conn.execute(query)._metadata.keys
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-27 21:26:12
Ten link pokazuje, jak uzyskać wszystkie potrzebne metadane dotyczące tabeli, kolumny i więcej.
Wiele z powyższych odpowiedzi opiera się na informacjach na tej stronie. Załóżmy, że zadeklarowaliśmy tabelę.
employees = Table('employees', metadata,
Column('employee_id', Integer, primary_key=True),
Column('employee_name', String(60), nullable=False),
Column('employee_dept', Integer, ForeignKey("departments.department_id"))
)
Oto kilka przykładów pobierania metadanych o tabeli.
# access the column "EMPLOYEE_ID":
employees.columns.employee_id
# or just
employees.c.employee_id
# via string
employees.c['employee_id']
# iterate through all columns
for c in employees.c:
print(c)
# get the table's primary key columns
for primary_key in employees.primary_key:
print(primary_key)
# get the table's foreign key objects:
for fkey in employees.foreign_keys:
print(fkey)
# access the table's MetaData:
employees.metadata
# access the table's bound Engine or Connection, if its MetaData is bound:
employees.bind
# access a column's name, type, nullable, primary key, foreign key
employees.c.employee_id.name
employees.c.employee_id.type
employees.c.employee_id.nullable
employees.c.employee_id.primary_key
employees.c.employee_dept.foreign_keys
# get the "key" of a column, which defaults to its name, but can
# be any user-defined string:
employees.c.employee_name.key
# access a column's table:
employees.c.employee_id.table is employees
# get the table related by a foreign key
list(employees.c.employee_dept.foreign_keys)[0].column.table
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-08-04 22:48:40
Po prostu
>>> q[0].keys()
Po
row_data = session.query(Projects).filter_by(id=1).one()
Przykład:
>>> q = session.query(users_user.phone,users_user.first_name).filter(users_user.phone=='79267548577').limit(1).all()
>>> columns_names = q[0].keys
Wynik:
>>> q[0].keys()
['phone', 'first_name']
>>>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-05-28 10:27:59
Chciałbym rozszerzyć odpowiedź @zzzeek. Rzeczywiście zapytanie ma atrybut column_descriptions
, ale nie jest dostępne dla wszystkich metod.
Rozważ następujące dwa zapytania:
1. query = session.query(Projects).filter_by(<filter_condition>)
2. query = session.query(Projects).all() <-- This query does not have column_descriptions.
Więc jeśli natkniesz się na sytuację, w której musisz użyć atrybutu column_descriptions
, ale używając ...query(...).all()
, możesz zmienić go na ...query(...).filter_by()
tj. filter_by()
bez żadnego warunku filtrowania.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-12-30 17:13:02