0
我有一個引用ForeignKeys和ManyToMany對象的基本模型。在「編輯」測試中,您正在採取視圖的ID並對其進行更改,但遇到了一個問題,我很好奇是否有其他人找到了更清晰的解決方法。我發現this後這讓我開始正確的道路Django測試 - 獲取初始值並將它們反饋回
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)
有其他人遇到了這個還是有更好的方法來解決這個問題?
這當然得到的你最的方式存在。尼斯。 – rh0dium 2012-01-19 12:40:03