我有一个简单的 API 函数,如下所示,
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
Run Code Online (Sandbox Code Playgroud)
我正在使用uvicorn命令启动服务器,
uvicorn main:app
Run Code Online (Sandbox Code Playgroud)
由于我们没有直接调用任何 python 文件,因此无法uvicorn从 Pycharm调用命令。
那么,如何使用 Pycharm 运行 fast-api 服务器?
我有一个用 FastAPI 编写的简单路线,如下所示,
from fastapi import FastAPI
app = FastAPI()
@app.get("/foo/bar/{rand_int}/foo-bar/")
async def main(rand_int: int):
return {"path": f"https://some-domain.com/foo/bar/{rand_int}/foo-bar/?somethig=foo"}
Run Code Online (Sandbox Code Playgroud)
我怎样才能“以编程方式”获取当前路径,
some-domain.com)/foo/bar/{rand_int}/foo-bar/)?somethig=foo)我已经djangorestframework==3.11.0从旧版本更新到了。现在我有这个错误,
类型错误:register() 得到了一个意外的关键字参数“base_name”
Traceback
...
...
...
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/abu/projects/django-example/django2x/urls.py", line 21, in <module>
path('sample/', include('sample.urls')),
File "/home/abu/.virtualenvs/django-example/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen …Run Code Online (Sandbox Code Playgroud) 我正在使用coverage.py来获取代码的测试覆盖率。
假设我在两个不同的模块中有两个同名的函数
# foo/foo.py
def get_something():
# fetch something
# 10 line of branch code
return "something foo/foo.py"
# bar/foo.py
def get_something():
# fetch something
# 20 line of branch code
return "something bar/foo.py"
Run Code Online (Sandbox Code Playgroud)
如何bar.foo.get_something(...)“完全”排除该功能?
如何Request从中间件函数为对象设置任意属性?
from fastapi import FastAPI, Request
app = FastAPI()
@app.middleware("http")
async def set_custom_attr(request: Request, call_next):
request.custom_attr = "This is my custom attribute"
response = await call_next(request)
return response
@app.get("/")
async def root(request: Request):
return {"custom_attr": request.custom_attr}
Run Code Online (Sandbox Code Playgroud)
此设置引发异常,
AttributeError:“请求”对象没有属性“custom_attr”
那么,如何获取"This is my custom attribute"路由器中的值呢?
我有一个 Django REST Framework 项目,它使用ModelViewSet为包含FileField.
我在这里分享了演示此问题的 Django 项目的完整示例。但总结一下关键组成部分:
模型.py
from django.db import models
class Profile(models.Model):
image = models.FileField(upload_to='uploads/%Y/%m/%d/')
Run Code Online (Sandbox Code Playgroud)
视图.py
from rest_framework import (
viewsets,
serializers,
parsers,
)
from sample import models
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = models.Profile
fields = ['id', 'image']
read_only_fields = ['id']
class ProfileViewSet(viewsets.ModelViewSet):
serializer_class = ProfileSerializer
queryset = models.Profile.objects.all()
Run Code Online (Sandbox Code Playgroud)
urls.py
from drf_spectacular.views import (
SpectacularAPIView,
SpectacularSwaggerView,
)
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from …Run Code Online (Sandbox Code Playgroud) 假设我有以下观点,
from fastapi import FastAPI
app = FastAPI()
@app.get('/hello/')
def hello_world():
return {"msg": "Hello World"}
@app.get('/hello/{number}/')
def hello_world_number(number: int):
return {"msg": "Hello World Number", "number": number}
Run Code Online (Sandbox Code Playgroud)
我一直在 Flask 和 Django 中使用这些函数
所以,我怎样才能获得/建立的网址,hello_world并hello_world_number以类似的方式?
我有一个docker-compose.yml看起来像
version: '2'
services:
redis:
image: redis
mysqldb:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=passme
- MYSQL_DATABASE=mydb
- MYSQL_USER=root
base:
build: .
volumes:
- .:/proj
environment:
- ENV_1=Value_1
- ENV_2=Value_2
- ENV_3=Value_3
worker:
extends:
service: base
command: celery -A proj worker --loglevel=debug
links:
- redis
- mysqldb
depends_on:
- mysqldb
web:
extends:
service: base
links:
- mysqldb
- redis
depends_on:
- mysqldb
ports:
- "8000:8000"
command: python manage.py runserver 0.0.0.0:8000
Run Code Online (Sandbox Code Playgroud)
该
extends关键字在早期撰写的文件格式,最高可达撰写文件支持version …
假设,我有一个代码片段
foo = SomeClass()
bar = foo[1:999].execute()Run Code Online (Sandbox Code Playgroud)
为了测试这一点,我尝试了一些
foo_mock = Mock()
foo_mock[1:999].execute()Run Code Online (Sandbox Code Playgroud)
不幸的是,这引发了一个例外,
类型错误:“模拟”对象不可下标
那么,如何创建可下标的 Mock对象?
我是 DJango 和 DRF 的新手,并被要求管理一些与 DJango/DRF 相关的代码。经过大量搜索,我仍然无法找到有关 filter_queryset 如何工作以及如何与不同参数一起使用的完整示例。
在某些地方,我看到它像下面这样使用,
self.filter_queryset(queryset)
Run Code Online (Sandbox Code Playgroud)
在其他地方,它与一些参数一起使用。如果有人可以解释基本原理,例如如何以及何时使用它,什么是因变量(lookup_field、filter_backends 等)和参数以及如何设置它们,那将会很有帮助。
我搜索了很多,也浏览了文档。如果我错过了任何文档,请告诉我。
python ×8
fastapi ×4
django ×2
coverage.py ×1
docker ×1
middleware ×1
mocking ×1
openapi ×1
pycharm ×1
python-3.x ×1
python-mock ×1
swagger-ui ×1
unit-testing ×1