PyCharm SQLAlchemy autocomplete

2019-04-08 11:28发布

I started evaluating PyCharm 3 professional edition because I will be working on several Pyramid + SQLAlchemy projects. One of the things I would really love to have is SQLAlchemy autocomplete.

I'll explain my scenario.

I created a new starter project with the alchemy scaffold, I followed the instructions here http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/pycharm/index.html. I also installed the SQLAlchemy package for the interpreter and virtual environment I am using for this project. Also, when I created a new pycharm project for this code, the IDE suggested me to install the pyramid, sqlalchemy and other packages. Of course I accepted the suggestion and let the IDE install all of those packages.

In the models.py file, the DBSession is declared as follows:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

In the views.py file, the DBSession is used this way:

one = DBSession.query(MyModel).filter(MyModel.name == 'one').first()

So I started playing with the IDE and did something like this: typed DBSession. and the IDE just gave me some few suggestions, within which the 'query' function was not listed. Then I tried typing: DBSession.query(MyModel). and pressed Ctrl+Space to try to get suggestions and a 'No suggestions' message showed up.

I would really like to have the SQLAlchemy suggestions of functions that I could use on my DBSession variable (like filter, filter_by, first, etc). I would say that this is mandatory for me :)

Is there something I am missing? Or, PyCharm doesn't support this?

2条回答
冷血范
2楼-- · 2019-04-08 11:40

Note that the tutorial states:

This guide was written for PyCharm 2.7.3, although many of the topics apply for PyCharm 3.

In PyCharm 3 Professional, it is much easier to install Pyramid and start using a scaffold. See one of my video tutorials Pyramid in PyCharm in 5 minutes at 1:17 specifically.

Also you might want to blow away your project and start fresh if stuff doesn't work as expected.

PyCharm 3 Professional supports SQAlchemy as follows.

  • Code insight (2.6+)
  • Possibility to view database structure in a diagram. Refer to the section Working with Diagrams.
  • Code completion and resolve. (3.0+)

See more information on how to use code completion.

查看更多
一夜七次
3楼-- · 2019-04-08 11:41

The solution I've found to this (picked up from somewhere on the web) was to type hint the DBSession instance like this:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
""":type: sqlalchemy.orm.Session"""

After this the code completion seems to work fine everywhere in the project

查看更多
登录 后发表回答