有问题的完整分支在这里:
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'])
...
接下来 - 我有一个单元测试,它通过一个fixture来播种一些Room对象,然后尝试提交一个提交到表单:
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&amp; 1.6.5 - SQL Lite和MySQL - Linux / Mac - Apache和本机Django服务器
一直是我的基线
答案 0 :(得分:0)
您可以将ChoiceField更改为ModelChoiceField
location = forms.ModelChoiceField(queryset=LocationItem.objects.all().order_by('room__name')
以下是一些可能有用的文档:https://docs.djangoproject.com/en/1.6/ref/forms/fields/#modelchoicefield