Django - 反向工程管理站点的“添加外键”按键 [英] Django - Reverse Engineering the Admin site's "Add Foreign Key" button

查看:172
本文介绍了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 ,外键模型称为区域模型列出系统有多少个区域(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 = 1id =add_id_numZonesonclick =return showAddPopup(this);> Add< / a>

在我的 New_Zone 视图中,保存新区域后,我检查弹出 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: p>

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天全站免登陆