I want to make the form used to filter searches without any field. For example, given this code:
models.py:
class Message(models.Model):
happened = models.DateTimeField()
filename = models.CharField(max_length=512, blank=True, null=True)
message = models.TextField(blank=True, null=True)
dest = models.CharField(max_length=512, blank=True, null=True)
fromhost = models.ForeignKey(Hosts, related_name='to hosts', blank=True, null=True)
TYPE_CHOICES = ( (u'Info', u'Info'), (u'Error', u'Error'), (u'File', u'File'), (u'BPS', u'BPS'),)
type = models.CharField(max_length=7, choices=TYPE_CHOICES)
job = models.ForeignKey(Jobs)
views.py:
WHEN_CHOICES = ( (u'', ''), (1, u'Today'), (2, u'Two days'), (3, u'Three Days'), (7, u'Week'),(31, u'Month'),)
class MessageSearch(ModelForm):
message = forms.CharField(max_length=25, required=False)
job = forms.CharField(max_length=25, required=False)
happened = forms.CharField(max_length=14, widget=forms.Select(choices=WHEN_CHOICES), required=False)
class Meta:
model = Message
Here is the code that I have now. As you can see, this makes a model based form. I redefined the message on the form because I use the icontains filter, so I did not need a giant text field. I revised the date mainly because I didnβt want to have to mess with dates (I hate working with dates! Who donβt?) And I changed the jobs field because otherwise I would get a drop-down list of existing tasks and I really wanted to be able to search by common words. Therefore, I was able to mark all those that are not required
, , .
. , , . . , , . , = false, Meta: model = ? , .
django, , - , :)