Upload CSV file in django admin list view, replacing add object button
<p>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. </p>

<p>In the list view I am struggling to override the form, as setting </p>

<pre><code>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
</code></pre>

<p>The admin change_list.html is overridden as follows:</p>

<pre><code>{% extends "admin/change_list.html" %}
{% load i18n admin_urls admin_static admin_list %}

{% block object-tools-items %}

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

<p>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.</p>

<p>cheers</p>

<p>Edited with sebb's input below. Thanks sebb.
The error fixed was </p>

<blockquote>
  <p>&lt; class âmy_model.admin.SomeModelAdmin'>: (admin.E016) The value of 'form' must inherit from 'BaseModelForm'</p>
</blockquote>

<p>to your class SomeModelForm add something like this:</p>

<pre><code>class Meta:
        model = YourModel
        fields = '__all__'
</code></pre>

<p>and change from forms.Form to forms.ModelForm</p>

python django file-upload admin