與問題的全科是在這裏:如何正確將表中的數據加載到表單中?
https://github.com/bethlakshmi/GBE2/tree/GBE-459
的問題是:
首先 - 我有需要在一個給定的數據對象有效的項目形式:
from [EventScheduleForm][1]:
class EventScheduleForm(forms.ModelForm):
required_css_class = 'required'
error_css_class = 'error'
day = forms.ChoiceField(choices=conference_days)
time = forms.ChoiceField(choices=conference_times)
location = forms.ChoiceField(choices=[
(loc, loc.__str__()) for loc in
LocationItem.objects.all().order_by('room__name')])
duration = DurationFormField(
help_text=scheduling_help_texts['duration'])
...
下一步 - 我有一個單元測試,通過夾具播種一些房間對象,然後嘗試提交到表單:
class TestEditEvent(TestCase):
'''Tests for edit_event view'''
''' Fixture to create some rooms, location items, and resource items '''
fixtures = ['scheduler/fixtures/rooms.json']
def setUp(self):
self.factory = RequestFactory()
self.s_event = factories.SchedEventFactory.create()
self.profile_factory = factories.ProfileFactory
self.client = Client()
self.room = Room.objects.all().order_by('name').first()
self.s_event.set_location(self.room)
...
def test_edit_event_submit_succeed(self):
'''edit event post succeeds without errors'''
profile = self.profile_factory.create()
form_post = self.get_edit_event_form()
request = self.factory.post('/scheduler/create/GenericEvent/%d' %
self.s_event.pk,
form_post)
request.user = profile.user_object
functions.grant_privilege(profile, 'Scheduling Mavens')
rooms = Room.objects.all().order_by('name')
for loc in rooms:
print "Room:" + loc.__str__() + "| \n"
response = edit_event(request, self.s_event.pk, "GenericEvent")
self.assertEqual(response.status_code, 200)
print(response.content)
self.assertFalse('<font color="red">!</font>' in response.content)
self.assertTrue(form_post['title'] in response.content)
self.assertTrue(form_post['description'] in response.content)
我發現的是代碼行:
location = forms.ChoiceField(choices=[
(loc, loc.__str__()) for loc in
LocationItem.objects.all().order_by('room__name')])
失敗。它的行爲好像沒有房間,但打印聲明顯示房間已正確設置並在單元測試中看到。
這條線有什麼問題?是否有更好的/不同的方式來加載房間的選擇列表,將工作不同?
它在集成中起作用 - 但我們遇到了麻煩,因爲我們必須重新啓動數據庫或者修改它以使代碼的這部分能夠正常工作 - 所以關於它如何從數據庫中提取。
其他說明: - Django的1.6 & 1.6.5 - SQL精簡版和MySQL - 的Linux/Mac的 - Apache和本地Django的服務器
一直是我的基線