Django 测试 - 获取初始值并将其反馈回来
我有一个引用ForeignKeys 和ManyToMany 对象的基本模型。在“编辑”测试中,您获取视图的 id 并对其进行更改,我遇到了一个问题,我很好奇是否有其他人找到了更干净的解决方法。我发现 这篇 帖子让我开始沮丧正确的道路
client = Client()
response = client.get(reverse("floorplan_update", kwargs={'pk': floorplan.id}))
data = response.context['form'].initial
# Ideally you should be able to do this..
response = client.post(reverse("floorplan_update", kwargs={'pk': floorplan.id}),
data=data, follow=True)
,但你不能这样做。如果你有 FK 或 M2M,你需要首先做这个丑陋的事情......
client = Client()
response = client.get(reverse("floorplan_update", kwargs={'pk': floorplan.id}))
data = response.context['form'].initial
# Ugliness ensues..
data['document'] = open(__file__)
data['company']= data['company'].id
data['target']= data['target'].id
# Only now can you post..
response = client.post(reverse("floorplan_update", kwargs={'pk': floorplan.id}),
data=data, follow=True)
还有其他人遇到过这个吗?或者有更好的方法来处理这个吗?
I have a basic model which references both ForeignKeys and ManyToMany objects. In "edit" testing where you are taking the id of a view and making changes to it I ran into a problem and I'm curious if anyone else figured out a cleaner workaround. I found this post which started me down the right path
client = Client()
response = client.get(reverse("floorplan_update", kwargs={'pk': floorplan.id}))
data = response.context['form'].initial
# Ideally you should be able to do this..
response = client.post(reverse("floorplan_update", kwargs={'pk': floorplan.id}),
data=data, follow=True)
But you can't do this. In cases where you have FK's or M2M's you need to first do this ugliness...
client = Client()
response = client.get(reverse("floorplan_update", kwargs={'pk': floorplan.id}))
data = response.context['form'].initial
# Ugliness ensues..
data['document'] = open(__file__)
data['company']= data['company'].id
data['target']= data['target'].id
# Only now can you post..
response = client.post(reverse("floorplan_update", kwargs={'pk': floorplan.id}),
data=data, follow=True)
Has anyone else ran into this or is there a better way to deal with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定,但你可以尝试这个:
Not sure, but you can try this instead: