I have this model:
class Aircraft(models.Model):
model = models.CharField(max_length=64, blank=True)
type = models.CharField(max_length=32)
extra = models.CharField(max_length=32, blank=True)
manufacturer = models.CharField(max_length=32)
engine_type = models.IntegerField("Engine Type", choices=ENGINE_TYPE, default=0)
cat_class = models.IntegerField("Category/Class", choices=CAT_CLASSES, default=1)
And I have a "find aircraft" page where the user is presented with a form where they can enter data that will be used to find all aircraft that fits their criteria. For instance the user can enter "boeing" into a textbox and "jet" into the engine_type
box, and it will display all boeing jets in the database. The way I'm doing this now is by this form:
class AircraftSearch(ModelForm):
search = forms.CharField(max_length=100, required=False)
class Meta:
model = Aircraft
fields = ('engine_type', 'cat_class', )
And then a (needlessly complex) view which converts the data from this form into a set of filter()
's which get added to Aircraft.objects.all()
. (Instead of having 4 seperate search fields for each CharField, I have combined them all into one search field.)
This all works, but with one problem. If the user wants to exclude engine type from their search criteria, then they're screwed because "Any" is not a valid choice for the engine type field. I'm going to have to create a new field/widget for engine type and category/class to include "Any", which kind of defeats the purpose of using a model view in the first place
I'm curious. Is there a better way? This seems like a very common task which has to have been tackled by someone else already, but a google search brings up nothing.