I'm trying to write a Django-subclassed ModelChoiceField class that knows how to load itself, but I'm not having much luck figuring out the pattern required. I don't want form classes that uses my field to have to set anything other than the label and required attributes; that is, I don't want to put duplicate queryset logic in every one of my forms that use this field.
So something like this, although this doesn't work because the queryset param isn't set in the field's constructor. I guess I could pass in "queryset=Test.object.none()", but I'd rather my forms not have to put that bogus code in there either.
class MyForm(forms.Form):
c = MyModelChoiceField(label='Test', required=False)
class MyModelChoiceField(forms.ModelChoiceField):
def __init__(self, *args, **kwargs):
super(MyModelChoiceField, self).__init__(*args, **kwargs)
self.queryset = Test.objects.filter(id__gt=1)
Are there examples out there for how to accomplish this? I'm sure there are - I just can't seem to find them with Google today.