预填充Django AdminSplitDateTime()

时间:2012-05-14 07:16:47

标签: django django-forms django-admin

我有一个自定义表单如下

class ticketform( BootstrapForm ):
    class Meta:
        layout = ( 
            Fieldset( "", "project", "manager", "cc_to", "urgency", "deadline", "subject", "steps", "result", "desc", "file" ),
        )

    project = forms.CharField( max_length = 100, label = "Project", help_text = "Project this request is related to" )
    manager = UserModelChoiceField( queryset = User.objects.filter( is_active = True ).order_by( 'first_name' ), label = "Manager",
                                   help_text = "Immediate superior or project manager", required = True )
    cc_to = UserMultipleSelectField( queryset = User.objects.filter( is_active = True ).order_by( 'first_name' ), label = "CC To",
                                    widget = widgets.FilteredSelectMultiple( "Users", is_stacked = True ), required = False )
    OPTIONS = ( 
              ( 'Critical', 'Critical' ),
              ( 'Major', 'Major' ),
              ( 'Minor', 'Minor' ),
              )
    urgency = forms.ChoiceField( choices = OPTIONS, label = "Urgency", help_text = "Urgency of the request" )
    deadline = forms.CharField( widget = widgets.AdminSplitDateTime(), label = "Deadline",
                               help_text = "When should this ticket be completed", required = True )
    subject = forms.CharField( max_length = 100, label = "Subject", help_text = "Ticket Subject" )
    steps = forms.CharField( widget = forms.Textarea, label = "Steps", help_text = "Reproducible error/feature Steps" )
    result = forms.CharField( widget = forms.Textarea, label = "Result", help_text = "Expected Result" )
    desc = forms.CharField( widget = forms.Textarea, label = "Description", help_text = "Detailed Description" )
    file = forms.FileField( label = "File", help_text = "Attach File Max size 10MB", required = False )

我在视图中有以下代码来填充视图的表单

def view_ticket( request, ticket_id ):
    ticket = Ticket.objects.filter( pk=ticket_id )[0]
    dict = {'project' : ticket.project,
            'manager' : ticket.manager.pk,
            'urgencry': ticket.urgency,
            'deadline': ticket.deadline,
            'subject' : ticket.subject,
            'steps'   : ticket.steps,
            'result'  : ticket.result,
            'desc'    : ticket.detailed_disc,
            'file'    : ticket.attachments,
            'cc_to'   : ticket.cc_to.all()
            }

    form = ticketform( dict )

    return render_to_response( 'form/request.html', {'form':form,
                                                         },
                                  mimetype="text/html", context_instance=RequestContext( request ) )

现在发生的事情是deadline字段没有填充(所有其他字段都填充得很好)。虽然我把它传递给字典中的值。有没有办法预先填充表单上的AdminSplitDateTime()字段?

1 个答案:

答案 0 :(得分:0)

设置表单的初始值,如jpic

所指出的那样