Django - 更改不同模型之前的确认页面

发布于 2024-11-18 01:31:26 字数 984 浏览 6 评论 0原文

我想在对选定对象进行更改之前(在管理员之外)创建一个确认页面。这些对象可以具有不同的模型(但一次只能有一个模型)。

这很像删除前管理中所做的事情。但管理代码很复杂,我还没有掌握它是如何完成的。

首先,我有几个表单以不同的方式过滤对象,然后我将查询集传递到操作/确认页面。我创建了一个表单工厂,以便我可以根据模型定义不同的查询集(如 Stackoverflow 上的另一个类似问题所示):

def action_factory(queryset):
    ''' Form factory that returns a form that allows user to change status on commissions (sale, lead or click)
    '''
    class _ActionForm(forms.Form):
        items = forms.ModelMultipleChoiceField(queryset = queryset, widget=forms.HiddenInput())
        actions = forms.ChoiceField(choices=(('A', 'Approve'), ('D' ,'Deny'), ('W' ,'Under review'), ('C' ,'Closed')))
    return _ActionForm

我在我的观点中使用它:

context['form']=action_factory(queryset)()

问题是 items 字段根本不会在 html 中显示-隐藏时的代码。当我删除 HiddenInput 小部件时,它会正确显示表单。

我不想显示选择字段,因为可能有数千个对象。我想要的只是“您想更改 1000 个对象的状态吗”以及一个弹出菜单和一个提交按钮。看起来很简单的问题,但我无法让它发挥作用。

如果有人对我当前的尝试有解决方案,我会很高兴听到他们是如何做到的。如果有一个更清洁、更好的解决方案那就更好了。

I'd like to create a confirmation page for selected objects before a change is made to them (outside the admin). The objects can be of different models (but only one model a time).

This is much like what is done in administration before deletion. But the admin code is complex and I haven't grasped how it is done there.

First I have severall forms that filter the objects differently and then I pass the queryset to the action / confirmation page. I have created a form factory so that I can define different querysets depending on model (as seen in another similiar question here at Stackoverflow):

def action_factory(queryset):
    ''' Form factory that returns a form that allows user to change status on commissions (sale, lead or click)
    '''
    class _ActionForm(forms.Form):
        items = forms.ModelMultipleChoiceField(queryset = queryset, widget=forms.HiddenInput())
        actions = forms.ChoiceField(choices=(('A', 'Approve'), ('D' ,'Deny'), ('W' ,'Under review'), ('C' ,'Closed')))
    return _ActionForm

Which I use in my view:

context['form']=action_factory(queryset)()

The problem is that the items field wont be displayed at all in the html-code when it is hidden. When I remove the HiddenInput widget it displays the form correctly.

I don't want to display the choice field since there can be thousands of objects. All I want to have is something like "Do you want to change the status of 1000 objects" and a popdown and a submit button. A simple enough problem it seems, but I can't get it to work.

If someone has a solution to my current attempt I would be glad to hear how they have done it. Even better would be if there is a cleaner and better solution.

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

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

发布评论

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

评论(1

甜是你 2024-11-25 01:31:26

我使用了错误的小部件。它应该是 MultipleHiddenInput 而不是 HiddenInput。

I used the wrong widget. It should be MultipleHiddenInput not HiddenInput.

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