我正在使用Django Rest Framework 3编写的Web API中的序列化器中的循环依赖性进行斗争.虽然我知道项目中的循环依赖几乎总是设计糟糕的标志,但我找不到一个好的方法来避免它使应用程序成为一个巨大的整体噩梦.
一个简单的剥离示例很好地描绘了在所有地方发生的事情,我遇到了类似的问题.
让我们在两个应用程序中有两个简单的模型:
# profiles/models.py
from images.models import Image
class Profile(models.Model):
name = models.CharField(max_length=140)
def recent_images(self):
return Image.objects.recent_images_for_user(self)
Run Code Online (Sandbox Code Playgroud)
# images/models.py
class Image(models.Model):
profile = models.ForeignKey('profiles.Profile')
title = models.CharField(max_length=140)
Run Code Online (Sandbox Code Playgroud)
遵循胖模型的原则,我经常在模型中使用多个导入,以便使用Profile上的方法轻松检索相关对象,但这很少会导致循环依赖,因为我很少从另一端做同样的事情.
当我尝试向串口添加序列化器时,问题就开始了.为了缩小API占用空间并将必要的调用量限制到最小,我想在其两端以一种简化形式序列化一些相关对象.
我希望能够在/profile端点上检索配置文件,这些配置文件将简化用户嵌套创建的最近几张图像的信息.此外,当从/images端点检索图像时,我希望在图像JSON中嵌入配置文件信息.
为了实现这一点并避免递归嵌套,我有两个序列化器 - 一个嵌套相关对象,一个不嵌套,对于这两个应用程序.
# profiles/serializers.py
from images.serializers import SimplifiedImageSerializer
class SimplifiedProfileSerializer(serializers.Serializer):
name = serializers.CharField()
class ProfileSerializer(SimplifiedProfileSerializer):
recent_images = SimplifiedImageSerializer(many=True)
Run Code Online (Sandbox Code Playgroud)
# images/serializers.py
from profiles.serializers import SimplifiedProfileSerializer
class SimplifiedImageSerializer(serializers.Serializer):
title = serializers.CharField()
class ImageSerializer(SimplifiedImageSerializer):
profile …Run Code Online (Sandbox Code Playgroud) django serialization circular-dependency python-import django-rest-framework
我只是运行py.test我的代码并得到以下输出:
================== 6 passed, 2 pytest-warnings in 40.79 seconds =======================
Run Code Online (Sandbox Code Playgroud)
但是,我看不出有什么py.test想提醒我的.如何打开警告输出到控制台?
py.test --help给我--strict一面旗帜:
- 严格模式下运行pytest,警告成为错误.
但是我只是想看看输出,而不是让我的测试失败.
我检查了pytest.org和这个问题,但他们只关心在python中声明警告,而不是在命令行上显示警告.
我想在我的Pycharm IDE中使用Tim Pope的vim 环绕插件.我一直在使用Pycharm的IdeaVim插件来使用vim运动和命令.
我知道我可以使用~/.ideavimrc像我正常的.vimrc,但我无法找到有关如何使用插件与ideavim信息.
我可以在我的内部指定插件目录,~/.ideavimrc还是必须采用其他方式?我可以像病原体一样使用插件管理器吗?
我使用Connexion的框架瓶打造的microService.我想用我的应用程序编写测试py.test.
pytest-flask它在文档中说创建一个夹具,conftest.py就像这样创建应用程序:
conftest.pyimport pytest
from api.main import create_app
@pytest.fixture
def app():
app = create_app()
return app
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我正在使用这样的client夹具:
test_api.pydef test_api_ping(client):
res = client.get('/status')
assert res.status == 200
Run Code Online (Sandbox Code Playgroud)
但是当我运行时,py.test我收到以下错误消息:
==================================== ERRORS ====================================
_______________________ ERROR at setup of test_api_ping ________________________
request = <SubRequest '_monkeypatch_response_class' for <Function 'test_api_ping'>>
monkeypatch = <_pytest.monkeypatch.MonkeyPatch instance at 0x7f9f76b76518>
@pytest.fixture(autouse=True)
def _monkeypatch_response_class(request, monkeypatch):
"""Set custom response class before test suite and restore the original …Run Code Online (Sandbox Code Playgroud) 我有几个我正在构建的网站,它们在页面中显示相同或相似的数据.为了减少代码重复,我所做的是创建了第3个项目/网站,并认为我将其用作共享HTML,java脚本,CSS,图像等的位置.
我可以使用相对路径从共享项目中包含JavaScript,CSS和图像.但是我无法呈现HTML的部分页面.当试图访问试图使用如下代码加载部分页面的两个网站时:
@RenderPage("/SharedArtifacts/Views/MySharedViewscshtml")
Run Code Online (Sandbox Code Playgroud)
我收到以下回复:
The virtual path '/SharedArtifacts/Views/MySharedViewscshtml' maps to another application, which is not allowed.
Run Code Online (Sandbox Code Playgroud)
现在我用谷歌搜索并尝试使用~前缀来指定解决问题的路径的根,但无济于事......仍然是同样的错误.
有关如何解决此问题的任何想法?
我需要在django基于类的视图中测试方法和辅助函数.
考虑这个基于类的视图:
class MyClassBasedView(View):
def dispatch(self, request, *args, **kwargs):
....
def __get_render_dict():
d = {}
...
return d
def my_method(self):
render_dict = self.__get_render_dict()
return render_response(self.request, 'template.html', render_dict)
Run Code Online (Sandbox Code Playgroud)
为了为我的视图编写单元测试,我需要调用里面的方法,__get_render_dict()直接说.我怎么能实现这个目标?
我试过了
v = MyClassedBasedView()
v.dispatch(request,args, kwargs)
v.__method_name()
Run Code Online (Sandbox Code Playgroud)
但是在post/get方法中没有匹配的参数失败了,即使我在不使用URL的情况下调用方法direclty.
我正在使用CircleCI,我想运行Huxley测试.
但为此我需要运行selenium服务器.
我试图运行selenium服务器独立jar.那不是解决方案.
如果你有所了解,请帮忙.
我有一个叫django的模型Blog.
我想在last_modified_date的当前模型中添加一个字段.我知道如何设置默认值,但我想以某种方式让它随时通过管理界面修改博客条目时自动更新.
是否有某种方法可以将此值强制为每个管理站点保存的当前时间?
还有一些方法可以添加一个mod_count字段,并在每次修改管理站点博客条目时自动计算它吗?
我想知道给 an 的数据UpdateView是什么时候保存的。
我有以下情况:我通过表单更新模型对象。我希望对模型的更改仅在 完成后才能UpdateView保存form_valid。但是,当我在 的开头打印对象属性时form_valid,它们已经更改为插入表单中的新属性。
那么,对模型的更改到底是什么时候保存的?如果我想对以前的值执行某些操作,我该怎么办?
我对 Django 比较陌生,我希望这个问题不会偏离轨道太远。
django ×5
python ×4
pytest ×2
testing ×2
asp.net ×1
asp.net-mvc ×1
circleci ×1
daemon ×1
django-admin ×1
flask ×1
huxley ×1
ideavim ×1
orm ×1
pycharm ×1
selenium ×1
swagger ×1
unit-testing ×1
unix ×1
vim ×1
vim-plugin ×1