我正在尝试Entry
使用tkinter
模块使用GUI中的按钮设置窗口小部件的文本.
该GUI用于帮助我将数千个单词分为五类.每个类别都有一个按钮.我希望使用一个按钮会显着加快我的速度,我想每次都仔细检查这些单词,否则我只会使用按钮并让GUI处理当前单词并带来下一个单词.
由于某种原因,命令按钮的行为不像我想要的那样.这是一个例子:
win = Tk()
v = StringVar()
def setText(word):
v.set(word)
a = Button(win, text="plant", command=setText("plant")
a.pack()
b = Button(win, text="animal", command=setText("animal"))
b.pack()
c = Entry(win, textvariable=v)
c.pack()
win.mainloop()
Run Code Online (Sandbox Code Playgroud)
到目前为止,当我能够编译时,点击什么都不做.
根据python模拟库的文档。我们从正在使用/调用它的模块中模拟出一个函数。
a.py
def function_to_mock(x):
print('Called function to mock')
b.py
from a import function_to_mock
def function_to_test(some_param):
function_to_mock(some_param)
# According to the documentation
#if we want to mock out function_to_mock in a
# test we have to patch it from the b.py module because that is where
# it is called from
class TestFunctionToTest(TestCase):
@patch('b.function_to_mock')
def test_function_to_test(self, mock_for_function_to_mock):
function_to_test()
mock_for_function_to_mock.assert_called_once()
# this should mock out function to mock and the assertion should work
Run Code Online (Sandbox Code Playgroud)
我陷入了一种无法确切说出如何模拟相关函数的情况。这是情况。
# some application
validators.py
def validate_a_field(value):
# do …
Run Code Online (Sandbox Code Playgroud) 我似乎无法找到如何刷新白噪声静态文件缓存。它一直给我带来一个问题。即使我删除了导致问题的特定文件,这种情况仍然存在。清单仍然提到丢失的已删除静态文件。我想清除缓存以便可以重新填充它。我正在使用带有 django 的 docker-container
什么是django表单的clean方法的序列化程序等效项。
我的用例是我想对模型序列化程序类的某些字段进行自定义模型字段验证。我的猜测可能是该框架的run_validators。但是我不确定。
谢谢,问候,
在 django 中创建测试的约定是将测试放置在名为 的模块中tests_*.py
,然后以python manage.py test
.
这将运行所有名为测试的模块中定义的测试。
我遇到的挑战是集成测试可能需要大量的资源设置,例如与外部服务的连接。我想在集成测试中模拟这些服务会导致集成测试失去其意义。
因此,我询问仅运行单元测试以及仅在单元测试正常运行时运行集成测试的最佳实践。
我能想到的唯一方法是放置integration tests
在以不同模式命名的文件中integration_*.py
,然后在运行django 文档指定的集成测试时使用模式参数
像这样python manage.py test --pattern="integration_*"
。
这样当python manage.py test
调用集成测试时就会被忽略。
有没有人有建议或者推荐。
我需要为已经存在的模型对象填充 AutoslugField。我的错误意识到 slug 字段非常方便,而且比使用 pk 用于安全目的更好。
我已经在数据库中有模型对象(行)。我想向它们添加 AutoSlugField。
任何人都知道我如何实现这一目标。
谢谢
我问这个问题是因为当我使用使用MPPT模型的django-hordak时,我遇到了django.db.utils.IntegrityError: null value in column \xe2\x80\x9clft\xe2\x80\x9d违反非空约束。
\n\n提出的一些解决方案虽然hacky
违背了惯例,但确实有效。
基础
\n\n在 django 中,当编写自定义迁移时,例如创建基线数据时。
\n\ndef forward_func(apps_registry, schema_editor):\n Model = apps_registry.get_model(\'app_name\', \'ModelName\')\n Model.objects.create(**{\'field_1\': \'field_1\', \'field_2\': \'field_2\')\n # Do whatever you want with my Model \n
Run Code Online (Sandbox Code Playgroud)\n\n在我的情况下,django-hordak 具有帐户模型。.objects.create(**data)\nraises the
上面指定的数据库上的 IntegrityError 。
建议的解决方案
\n\n建议的解决方案之一是直接导入模型。
\n\ndef forward_func(apps_registry, schema_editor):\n # Model = apps_registry.get_model(\'app_name\', \'ModelName\')\n # lets forget about importing the standard way because it will raise integrity error.\n\n from app_name.models import …
Run Code Online (Sandbox Code Playgroud) django ×6
python ×3
unit-testing ×2
django-forms ×1
docker ×1
events ×1
import ×1
mocking ×1
tkinter ×1
whitenoise ×1