I'm trying to make a custom form field in Django that allows a user to link a file or upload a file. To do this, I'm creating a subclass of the MultiValueField with the fields property set to (URLField(), FileField()). I'm not sure this is the right approach, but I keep getting an error I can't understand:
'MyFileField' object has no attribute 'attrs'
Here is my code. Can anyone explain what's going on?
from django import forms
from django.core.exceptions import ValidationError
from .models import Case, Person, Opp, Category
class MyFileField(forms.MultiValueField):
def compress(self, data_list):
# I'll get to this once the attr error goes away
pass
def __init__(self, *args, **kwargs):
fields = (
forms.URLField(), forms.FileField()
)
super(MyFileField, self).__init__(fields=fields, *args, **kwargs)
class CaseForm(forms.ModelForm):
class Meta:
model = Case
fields = ['title', 'file', 'categories']
widgets = {
'file': MyFileField
}