Django - 对管理站点的“添加外键"进行逆向工程;按钮 [英] Django - Reverse Engineering the Admin site's "Add Foreign Key" button

查看:21
本文介绍了Django - 对管理站点的“添加外键"进行逆向工程;按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR(简短概要):

我在自己的项目中重新创建了管理员添加"按钮.但是,当我在父表单上点击保存"时,它无法识别新的选择元素.

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

整个故事:

我有这个功能在我自己的项目中工作......几乎.我需要帮助弄清楚最后一步.就像现在一样,我有一个+"按钮,我点击它,一个弹出窗口出现,我添加一个新对象,点击保存,弹出窗口关闭,这个新项目现在在我的选择框中并被选中 - 就像管理员一样页.但是,当我在这个父表单上点击保存时,我收到错误消息,我选择了一个不在列表中的项目.当然,由于页面已经重新加载,我的新项目是列表的一部分,我只需再次点击保存就可以了.当然,我需要它来保存第一次!

I have that functionality working in my own project... almost. I need help figuring out the last step. As it is now, I have a "+" button, I click it, a popup shows up, I add a new object, hit save, popup closes and that new item is now in my select box and selected - just like the admin page. However, when I hit save on this parent form, I get the error that I've selected an item not in the list. Of course, since the page has reloaded, my new item is part of the list and I just hit save again and it works. Of course, I need it to save on the first time!

基本设置是我的父模型称为System,外键模型称为Zone.Zone 模型列出了一个系统有多少个区域(1 个区域、2 个区域、10 个区域等...)

The basic setup is my Parent model is called System and the foreign key model is called Zone. The Zone model lists how many zones a system has (1 zone,2 zones,10 zones, etc...)

好的,一些代码:

父表单模板中的添加"链接:

The "Add" link in the template of the parent form:

<a href="/systems/zones/new/?popup=1" id="add_id_numZones" onclick="return showAddPopup(this);">Add</a>

在我的 New_Zone 视图中,保存新区域后,我检查 popup GET 变量是否为 1,如果是,则返回一个javascript 函数.这是视图:

In my New_Zone view, after saving the new zone, I check if the popup GET variable is 1, if so, return a javascript function. Here's the view:

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

这是我的 Javascript(主要是从管理 javascript 复制的:

And here is my Javascript (largely copied from the admin javascript:

function html_unescape(text) {
// Unescape a string that was escaped using django.utils.html.escape.
    text = text.replace(/&lt;/g, '<');
    text = text.replace(/&gt;/g, '>');
    text = text.replace(/&quot;/g, '"');
    text = text.replace(/&#39;/g, "'");
    text = text.replace(/&amp;/g, '&');
    return text;
}

function windowname_to_id(text) {
    text = text.replace(/__dot__/g, '.');
    text = text.replace(/__dash__/g, '-');
    return text;
}


function showAddPopup(triggeringLink, pWin) {
    var name = triggeringLink.id.replace(/^add_/, '');
    href = triggeringLink.href;
    var win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
    win.focus();
    return false;
}

function closeAddPopup(win, newID, newRepr) {
    newID = html_unescape(newID);
    newRepr = html_unescape(newRepr);
    var name = windowname_to_id(win.name);
    var elem = document.getElementById(name);
    if (elem) {
        if (elem.nodeName == 'SELECT') {
            var o = new Option(newRepr, newID);
            elem.options[elem.options.length] = o;
            o.selected = true;
        } else if (elem.nodeName == 'INPUT') {
            if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
                elem.value += ',' + newID;
            } else {
                elem.value = newID;
            }
        }
    } else {
        var toId = name + "_to";
        elem = document.getElementById(toId);
        var o = new Option(newRepr, newID);
        SelectBox.add_to_cache(toId, o);
        SelectBox.redisplay(toId);
    }

    win.close();
}

我看了这个 问题似乎我正在做同样的事情.

I took a look at this question and it seems that I am doing precisely the same thing.

关于如何让表单识别新的选择元素的最后一步有什么想法吗?

Any ideas on how to get that last step of getting the form to recognize the new select element?

谢谢!

推荐答案

我想通了

问题是我传递给我的 closeAddPopup javascript 函数的内容.本质上,我传递的是垃圾值.

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

这是我在 New_Zone 视图中的原始内容(不起作用):

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

对我来说这是一个非常愚蠢的错误(显然已经晚了).我将 f 分配给字段 numOfZones认为pk 并将其发送到脚本.

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)))

无论如何...感谢...好吧,Stackoverflow.如果不发布问题并在 stackoverflow 上重新阅读我的代码,我认为我不会解决问题.

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.

这篇关于Django - 对管理站点的“添加外键"进行逆向工程;按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆