Django - 如何从数据库动态填充formwizard表单的字段

时间:2016-09-07 18:48:51

标签: python django django-models django-forms

def get_form_initial(self, step):
    if 'project_id' in self.kwargs:
        if step == 'project':
            project_id = self.kwargs['project_id']
            project = Project.objects.get(pro_id=project_id)
            from django.forms.models import model_to_dict
            project_dict = model_to_dict(project)
            return project_dict
        elif step == 'task':
            project_id = self.kwargs['project_id']
            tasks = Task.objects.filter(project=project_id)
            print "tasks: %s " % tasks
            pp.pprint(self.__dict__)
            tasks_dict = model_to_dict(tasks)
            return tasks_dict
        else:
            print self.kwargs
    else:
        return self.initial_dict.get(step, {})

第一个表单不是FormSet,可以在第一步project轻松填充,但第二个步骤是FormSet。如何检索Task中的FormSet表单,并使用表单及其各自的字段填充向导步骤中的FormSet

tasks: [<Task: Task object>, <Task: Task object>] 
{   'args': (),
    'condition_dict': {   },
    'form_list': OrderedDict([(u'project', <class 'proposed_project_details.forms.Project_Form'>), (u'task', <class 'django.forms.formsets.Task_FormFormSet'>), (u'resources', <class 'django.forms.formsets.Resources_Required_FormFormSet'>), (u'deliverable', <class 'django.forms.formsets.DeliverableFormFormSet'>), (u'staffing', <class 'django.forms.formsets.Staffing_FormFormSet'>)]),
    'head': <bound method OrderWizard.get of <OrderWizard: forms: OrderedDict([(u'project', <class 'proposed_project_details.forms.Project_Form'>), (u'task', <class 'django.forms.formsets.Task_FormFormSet'>), (u'resources', <class 'django.forms.formsets.Resources_Required_FormFormSet'>), (u'deliverable', <class 'django.forms.formsets.DeliverableFormFormSet'>), (u'staffing', <class 'django.forms.formsets.Staffing_FormFormSet'>)])>>,
    'initial_dict': {   },
    'instance_dict': {   },
    'kwargs': {   'project_id': u'6'},
    'prefix': 'order_wizard',
    'request': <WSGIRequest: POST '/projects/edit/6'>,
    'steps': <StepsHelper for <OrderWizard: forms: OrderedDict([(u'project', <class 'proposed_project_details.forms.Project_Form'>), (u'task', <class 'django.forms.formsets.Task_FormFormSet'>), (u'resources', <class 'django.forms.formsets.Resources_Required_FormFormSet'>), (u'deliverable', <class 'django.forms.formsets.DeliverableFormFormSet'>), (u'staffing', <class 'django.forms.formsets.Staffing_FormFormSet'>)])> (steps: [u'project', u'task', u'resources', u'deliverable', u'staffing'])>,
    'storage': <formtools.wizard.storage.session.SessionStorage object at 0x10e138a90>}

0 个答案:

没有答案