Django - Reverse Engineering the Admin site's “Add Foreign Key” button

后端 未结 1 1292
醉酒成梦
醉酒成梦 2020-12-17 04:17

TL;DR (Short synopsis):

I have recreated the admin \"Add\" button in my own project. However, when I hit \"save\" on the parent form, it is not rec

相关标签:
1条回答
  • 2020-12-17 05:06

    I Figured it Out

    The problem was what I was passing to my closeAddPopup javascript function. Essentially, I was passing garbage values.

    Here's what I originally had in my New_Zone view (which didn't work):

        ...
        if form.is_valid():
            f = form.save(commit=False)
            pk_value = f.numOfZones
            form.save()
            obj = Zone_Info.objects.get(numOfZones=pk_value)
            if isPopup == "1":
                return HttpResponse('<script>opener.closeAddPopup(window, "%s", "%s");</script>' % (escape(pk_value), escape(obj)))
        ...
    

    It's a pretty stupid mistake on my part (clearly it's late). I was assigning f to the field numOfZones which I thought was the pk and sending that to the script.

    Now, the working view looks like this:

           if form.is_valid():
               obj = form.save()
               pk_value = obj.pk
               if "_popup" in request.REQUEST:
                   return HttpResponse('<script>opener.closeAddPopup(window, "%s", "%s");</script>' % (escape(pk_value), escape(obj)))
    

    Anyway... thanks to... well, Stackoverflow. I don't think I would have solved the problem without posting the question and rereading my code on stackoverflow.

    0 讨论(0)
提交回复
热议问题