开发者

Redirect to a new form in Django Admin on Save

开发者 https://www.devze.com 2023-01-06 19:34 出处:网络
Hi I am creating a trouble ticket app for my company, and I want to redirect the user to a new form where he will specify the diagnose and solution he offered.

Hi I am creating a trouble ticket app for my company, and I want to redirect the user to a new form where he will specify the diagnose and solution he offered. my admin is Basically, Right now my code is calling the first form, when the obj created is new or the status is open and it is calling the ClosedForm when my status is closed.

What I want is that, when the user changes the status from Open to Closed and saves the ticekt, he is redirected to ClosedForm

Thanks

class TicketFormClosed(ModelForm):
    class Meta:
        model = Ticket
        fields = ('status',开发者_运维技巧'call_sheet_number','diagnose','solution','call_attend_date',)

class TicketForm(ModelForm):
    class Meta:
        model = Ticket
        exclude = ('call_sheet_number','diagnose','solution','call_attend_date',)

class TicketAdmin(admin.ModelAdmin):

    def get_form(self, request, obj=None, **kwargs):
        form = super(TicketAdmin, self).get_form(request, obj, **kwargs)
        if obj == None or obj.status=='Open':
            form = TicketForm   
        else:
            form = TicketFormClosed
        return form


Take a look at the source code for admin.ModelAdmin. You will find the overridable hooks for redirecting there.

for example, search for HttpResponseRedirect in django/contrib/admin/options.py and you'll see the default flow playing out.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号