在Python Shell中保存django CreateView
我正在尝试在 django 中为我的 CreateView 视图编写一个测试。作为测试的一部分,我想通过 CreateView 类创建一个新对象,尽管我不确定如何通过tests.py 保存该对象。
models.pyviews.pytests.py
class MyModel(models.Model):
name = models.CharField(
max_length = 50,
)
m.save
class MyCreateView(CreateView):
model = MyModel
() 和 m.submit (
from myapp.views import MyCreateView
m = MyCreateView()
m.name = 'John Doe'
# save object here
) 都不起作用。有什么建议吗?
I'm trying to write a test for my CreateView view in django. As part fo the test, I want to create a new object through a CreateView class, though I'm unsure how to save the object through tests.py.
models.py
class MyModel(models.Model):
name = models.CharField(
max_length = 50,
)
views.py
class MyCreateView(CreateView):
model = MyModel
tests.py
from myapp.views import MyCreateView
m = MyCreateView()
m.name = 'John Doe'
# save object here
Neither m.save() nor m.submit() will work. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 Django 文档: https://docs.djangoproject.com/en/4.0 /topics/testing/
它有很好的文档记录并展示了如何测试视图和模型。
Please refer to Django docs: https://docs.djangoproject.com/en/4.0/topics/testing/
It is well documented and shows how to test views and models.