在Django管理员列表视图中上传CSV文件,替换“添加对象"按钮 [英] Upload CSV file in django admin list view, replacing add object button

查看:51
本文介绍了在Django管理员列表视图中上传CSV文件,替换“添加对象"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换管理页面列表视图中的添加对象按钮.其基本思想是,管理员可以下载数据库中所有模型上的数据,使用工具来编辑数据,然后以CSV文件的形式重新上传.

I want to replace the add object button in the listview of an admin page. The underlying idea is that an administrator can download data on all models in the db, use a tool to edit the data, and then reupload as a CSV file.

在列表视图中,我正在努力覆盖表单,将其设置为

In the list view I am struggling to override the form, as setting

class SomeModelForm(forms.Form):
    csv_file = forms.FileField(required=False, label="please select a file")

class Meta:
    model = MyModel
    fields = '__all__'

class SomeModel(admin.ModelAdmin):
    change_list_template = 'admin/my_app/somemodel/change_list.html'

    form = SomeModelForm

    other stuff

管理员change_list.html如下重写:

The admin change_list.html is overridden as follows:

{% extends "admin/change_list.html" %}
{% load i18n admin_urls admin_static admin_list %}

{% block object-tools-items %}

    <form action="{% url 'admin:custom_submit_row' %}" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <p>
            {{ form.as_p }}
        </p>
        <p><input type="submit" value="Upload" /><input type="reset" value="Reset"></p>
    </form>
{% endblock %}

以前,SomeModel缺少类Meta,因为根据sebbs响应,此更新.原始错误已解决,但现在管理页面正在显示上传和重置按钮,但没有用于文件上传的字段.

Previously SomeModel was missing the class Meta, as per sebbs response this is updated. The original error has been resolved but now currently the admin page is displaying the upload and reset buttons but no field for file uploads.

欢呼

使用下面的sebb输入进行编辑.谢谢sebb.已解决的错误是

Edited with sebb's input below. Thanks sebb. The error fixed was

<类"my_model.admin.SomeModelAdmin">:(admin.E016)"form"的值必须继承自"BaseModelForm"

< class ‘my_model.admin.SomeModelAdmin'>: (admin.E016) The value of 'form' must inherit from 'BaseModelForm'

推荐答案

此处为OP,解决方案如下:

OP here, solution is as follows:

class SomeModelForm(forms.Form):
    csv_file = forms.FileField(required=False, label="please select a file")


class SomeModel(admin.ModelAdmin):
    change_list_template = 'admin/my_app/somemodel/change_list.html'

    def get_urls(self):
        urls = super().get_urls()
        my_urls = patterns("",
                           url(r"^upload_csv/$", self.upload_csv, name='upload_csv')
                       )
        return my_urls + urls

    urls = property(get_urls)

    def changelist_view(self, *args, **kwargs):
        view = super().changelist_view(*args, **kwargs)
        view.context_data['submit_csv_form'] = SomeModelForm
        return view

    def upload_csv(self, request):
        if request.method == 'POST':
            form = MineDifficultyResourceForm(request.POST, request.FILES)
            if form.is_valid():
                # process form

模板被这样覆盖:

{% extends "admin/change_list.html" %}
{% load i18n admin_urls admin_static admin_list %}

{% block object-tools %}
    {% if has_add_permission %}
        <div>
            <ul class="object-tools">
                {% block object-tools-items %}
                    <form id="upload-csv-form" action="{% url 'admin:upload_csv' %}" method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                        <p>{{ form.non_field_errors }}</p>
                        <p>{{ submit_csv_form.as_p }}</p>
                        <p>{{ submit_csv_form.csv_file.errors }}</p>
                        <p><input type="submit" value="Upload" />
                            <input type="reset" value="Reset"></p>
                    </form>
                {% endblock %}
            </ul>
        </div>
     {% endif %}
{% endblock %}

该表单需要一些自定义验证,但这可以解决自定义管理页面的难题.

The form needs some custom validation but otherwise this solves the difficult part of customizing the admin page.

要详细说明这里发生的事情:

To elaborate what is going on here:

  1. get_urls被覆盖,以便可以向管理页面添加其他端点,该端点可以指向任何视图,在这种情况下,它指向upload_csv

  1. get_urls is overridden so that an additional endpoint can be added to the admin page, this can point to any view, in this case it points upload_csv

changelist_view被覆盖以将表单信息附加到视图

changelist_view is overridden to append the form info to the view

change_list.html模板块对象工具"被表单字段覆盖

the change_list.html template block "object-tools" is overridden with the form fields

希望其他人也能找到帮助.

Hopefully someone else finds this helpful as well.

这篇关于在Django管理员列表视图中上传CSV文件,替换“添加对象"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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