如何创建需要额外信息的复杂管理操作?

发布于 2024-09-17 12:48:50 字数 2620 浏览 4 评论 0原文

我有兴趣为管理界面创建一个操作,该操作需要除所选项目之外的一些附加信息。我的示例是将漫画批量添加到系列中。 (是的,我知道明显的答案是创建一个具有 X-to-X 关系的模式,但请耐心听我举一个简单的例子)。

在此示例中,我创建了 100 幅漫画。创建它们后,我想将它们与已创建的系列对象相关联。要在管理员中执行此操作,我想选择项目然后启动该操作。然后我应该被问到要使用哪个系列对象(通过弹出窗口、中间表单等)。

我已按照此处的说明进行操作声称通过中间形式来实现这一点。使用它之后,我没有再收到任何错误,但操作本身也没有被执行 - forloop 永远不会被执行。相反,它会返回漫画管理列表,并显示消息:“未选择任何操作。”

我的 admin.py 方法:

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
def addSeries(self, request, queryset):
    form = None
    if 'cancel' in request.POST:
        self.message_user(request, 'Canceled series linking.')
        return
    elif 'link_series' in request.POST:
        form = self.SeriesForm(request.POST)
        if form.is_valid():
            series = form.cleaned_data['series']
            for x in queryset:
                y = Link(series = series, comic = x)
                y.save()
            self.message_user(request, self.categorySuccess.render(Context({'count':queryset.count(), 'series':series})))
            return HttpResponseRedirect(request.get_full_path())
    if not form:
        form = self.SeriesForm(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})
    return render_to_response('setSeries.html', {'comics': queryset, 'form': form, 'path':request.get_full_path()}, context_instance=RequestContext(request))
addSeries.short_description = 'Set Series'

我的中间形式 setSeries.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Create Series Links</title>
    </head>
    <body>
        <h1>Create Series Links</h1>
        <p>Choose the series for the selected comic(s):</p>
        <form method="post" action="{{ path }}">
            <table>
                {{ form }}
            </table>
            <p>
                <input type="hidden" name="action" value="changeSeries" />
                <input type="submit" name="cancel" value="Cancel" />
                <input type="submit" name="link_series" value="link_series" />
            </p>
        </form>
        <h2>This categorization will affect the following:</h2>
        <ul>
            {% for comic in comics %}
                <li>{{ comic.title }}</li>
            {% endfor %}
        </ul>
    </body>
</html>

I'm interested in creating an action for the admin interface that requires some additional information beyond the items selected. My example is bulk adding comics to a series. (Yes I know the obvious answer is to create a schema with X-to-X relationships, but bear with me for the sake of a simple example).

In this example, I've created 100 comics. After they're created, I'd like to associate them with a series object that's already been created. To execute this action within the admin, I'd like to select the items then initiate the action. I should then be asked which series object to use (via a popup, intermediate form, etc.).

I've followed the instructions here which claim to accomplish this via an intermediate form. After working with it, I'm not getting any more errors, but the action itself isn't being executed either - the forloop never gets executed. Instead, it returns to the admin list of comics with the message: "No action selected."

my admin.py method:

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
def addSeries(self, request, queryset):
    form = None
    if 'cancel' in request.POST:
        self.message_user(request, 'Canceled series linking.')
        return
    elif 'link_series' in request.POST:
        form = self.SeriesForm(request.POST)
        if form.is_valid():
            series = form.cleaned_data['series']
            for x in queryset:
                y = Link(series = series, comic = x)
                y.save()
            self.message_user(request, self.categorySuccess.render(Context({'count':queryset.count(), 'series':series})))
            return HttpResponseRedirect(request.get_full_path())
    if not form:
        form = self.SeriesForm(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})
    return render_to_response('setSeries.html', {'comics': queryset, 'form': form, 'path':request.get_full_path()}, context_instance=RequestContext(request))
addSeries.short_description = 'Set Series'

My intermediate form setSeries.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Create Series Links</title>
    </head>
    <body>
        <h1>Create Series Links</h1>
        <p>Choose the series for the selected comic(s):</p>
        <form method="post" action="{{ path }}">
            <table>
                {{ form }}
            </table>
            <p>
                <input type="hidden" name="action" value="changeSeries" />
                <input type="submit" name="cancel" value="Cancel" />
                <input type="submit" name="link_series" value="link_series" />
            </p>
        </form>
        <h2>This categorization will affect the following:</h2>
        <ul>
            {% for comic in comics %}
                <li>{{ comic.title }}</li>
            {% endfor %}
        </ul>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2024-09-24 12:48:50

我注意到的一件事是,您的操作方法是“addSeries”,但在形式中您将其称为“changeSeries”。

在你的 ModelAdmin 中,你应该有这样一行:

actions = ['addSeries']

如果这是你的行,那么你需要将: 更改

<input type="hidden" name="action" value="changeSeries" />

为:

<input type="hidden" name="action" value="addSeries" />

这就是 Django 的管理员如何知道选择了哪个操作。当您在选择操作和执行操作之间有一个中间表单时,您需要从管理界面上的选择菜单中保留操作名称。

One thing I notice is that your action’s method is “addSeries”, but in the form you’re calling it “changeSeries”.

In your ModelAdmin, you should have a line like this:

actions = ['addSeries']

If that’s the line you have, then you need to change:

<input type="hidden" name="action" value="changeSeries" />

to:

<input type="hidden" name="action" value="addSeries" />

That’s how Django’s admin knows which action was selected. When you have an intermediary form between choosing the action and performing the action, you’ll need to preserve the action name from the select menu on the admin interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文