r/FastAPI • • Aug 21 '21

Question Shell for Fast api ?

There is shell in django , My teams wants the same for Fast api. I tried with IPython but the problem is I can't use Tortoise orm Queries as it says database has no connection and other stuff. My question : Is there any way to make or install a shell for fast api project.

11 Upvotes

12 comments sorted by

3

u/pytheous1988 Aug 22 '21

I guess my first question would be, what do you need the shell for? It's more likely you are trying to interact with the database in some way. The best thing to do would be to build out commands in click to your ORM layer or choice that does what you need it to do.

1

u/Impressive-Hat1494 Aug 22 '21

can you tell, how can I do that ?

2

u/pytheous1988 Aug 22 '21

Click is a library that allows you to build CLI commands easily. You use it to interact with the ORM. So say you needed to add a new user. You could build a command that is like python ORM.py create_user first_name last_name email and under the hood it uses the ORM commands to take those arguments and insert a new user row into your db.

2

u/wrtbwtrfasdf Aug 22 '21

Ipython will work just fine for running orm commands. All you need is a file that properly sets up the connection and models.

1

u/Impressive-Hat1494 Aug 22 '21

can you tell me little more how to do it for tortoise orm ?

6

u/sasmariozeld Aug 21 '21

i honestly cannot come up with a common valid use case for a fastapi cli in the last 5 minutes

1

u/Particular-Union3 Aug 22 '21

A really strange approach to bash scripting maybe?

1

u/sanketp60 Oct 16 '22

Try using pdb.

Add following lines to your code (preferably in your main app after imports):

import pdb
pdb.set_trace()

Hope that helps :)

1

u/[deleted] Aug 22 '21

There's not a "shell" with fastapi because the philosophy of the framweork is different than Django's monolithic philosophy. Django provides its orm. Fastapi wants to be "agnostic". It is up to you to chose the orm and how to interact with it.

0

u/[deleted] Aug 22 '21

[deleted]

1

u/Impressive-Hat1494 Aug 22 '21

I want to access database also.