django - 如何正确地将数据从表加载到表单?

标签 django orm

有问题的完整分支在这里:

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'])
...

接下来 - 我有一个单元测试,它通过一个装置为一些 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 & 1.6.5
- SQL Lite 和 MySQL
- Linux/Mac
- Apache 和 native Django 服务器

一直是我的基线

最佳答案

您可以将您的 ChoiceField 更改为 ModelChoiceField

     location = forms.ModelChoiceField(queryset=LocationItem.objects.all().order_by('room__name')

以下是一些可能有帮助的文档:https://docs.djangoproject.com/en/1.6/ref/forms/fields/#modelchoicefield

关于django - 如何正确地将数据从表加载到表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32263075/

相关文章:

python - sqlalchemy/sql : cross model relationships, 从 'cousin' 模型获取信息

java - JPA中的mappedBy或Hibernate中的inverse ="true"有何作用?

Python:使用 Pony ORM 进行简单排序

python - Tornado websockets : share open web sockets between processes

python - 如何在 Django 中检查匹配列表?

javascript - Django 单选按钮

collections - ColdFusion:具有多个外键的 ORM 集合

azure - 访问 Azure 表存储表时使用字符串名称

django - 如何使用 Django 编写 Facebook 应用程序?

python - 如何在 Django 中发送文件到响应?