How to load data from a table into a form?

The full branch with problems is here:

https://github.com/bethlakshmi/GBE2/tree/GBE-459

The problem is this:

First, I have a form that requires valid elements in a given data object:

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

Further. I have a unit test, which is the seed of some object objects through a fixture, and then tries to pass submit to the form:

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)

I find the line of code:

location = forms.ChoiceField(choices=[
            (loc, loc.__str__()) for loc in
            LocationItem.objects.all().order_by('room__name')])

Failure. It acts as if there are no numbers, but print statements show that the room is correctly installed and visible in the unit test.

What is wrong with this line? Is there a better / different way to download a list of room options that will work differently?

- , - - , .

: - Django 1.6 1.6.5 - SQL Lite MySQL - Linux/Mac - Apache Django

+4
1

ChoiceField ModelChoiceField

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

, : https://docs.djangoproject.com/en/1.6/ref/forms/fields/#modelchoicefield

0

All Articles