How include a filefield in a django admin model form that is not in the model

django, django-admin, django-forms, django-models, python

Solution

You should create a ModelForm for this model, and add the field there. It could look like this:

from django import forms

from models import MyModel

class MyModelForm(forms.ModelForm):
    extra_file = forms.FileField()

    class Meta:
        model = MyModel

Then, you can make the ModelAdmin to use this form. If you saved MyModelForm in yourapp/forms.py, your ModelAdmin would look like this:

from django.contrib import admin

from models import MyModel
from forms import MyModelForm

class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm
admin.site.register(MyModel, MyModelAdmin)

Problem

I want to include a filefield in an admin model form that will be used to uploaded a file that will then be read and the contents will be used to update other fields in the same model. After processing the file itself is not needed, so i don't want the filefield in the model just in the form. I have no problem overriding save and processing the form myself, but I can't figure out how to include a filefield in my form that is not in the model.

Original source