In SQLAlchemy, we can make queries like so:
session.query(Student).filter(Student.name == 'John')
# ________^^^
In this case, i can see the type of field Student.name
, IDE can support me with autocomplete.
Full code example
from sqlalchemy import create_engine
from sqlalchemy import MetaData, Column, String
metadata = MetaData()
Base = declarative_base(metadata=metadata)
engine = create_engine("sqlite:///db.sqlite3")
Session = sessionmaker(engine)
class Student(Base):
__tablename__ = "students"
token = Column(String, primary_key=True)
name = Column(String, nullable=True)
def __repr__(self):
return repr_helper(self)
metadata.create_all(engine)
with Session() as session, session.begin():
student = session.query(Student).filter(Student.name == 'John')
print(student.name)
But, in Django ORM:
Student.objects.filter(name='John')
# ^^^
In this case, name
it’s just an argument of filter function, filter expects any argument name and any argument value. It’s can’t be annotated.
Are there any tools, extensions to IDE, or plugins for static analyzers like mypy that would allow type checking and/or autocompletion of model fields, considering that there may be relational fields with related models, like Student.objects.filter(group__number='1a')
?