Django form without required fields

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): #Class that makes a form from a model that can be customized by placing info above the class Meta
        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, , - , :)

+3
3

ModelForm, . ModelForm :

from django.forms import ModelForm

class SearchForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(SearchForm, self).__init__(*args, **kwargs)
        for key, field in self.fields.iteritems():
            self.fields[key].required = False

, , ModelForm, :

class MessageForm(SearchForm):
    class Meta:
        model = Message
+5

empty_permitted=True , ,

form = MessageSearch(empty_permitted=True)

, - .

+2

django-filter:

http://django-filter.readthedocs.io/en/develop/

. . :

import django_filters

class MessageSearch(django_filters.FilterSet):

    class Meta:
        model = Message
        fields = ['happened', 'filename', 'message', '...', ]

    # django-filter has its own default widgets corresponding to the field
    # type of the model, but you can tweak and subclass in a django way :
    happened = django_filters.DateFromToRangeFilter()

, , ..

also: set the filter in the opposite way (the external key is not in the filtered model: the model is mentioned elsewhere in another table), simply, just name the table in which the foreign key of the filtered model field is:

    # the 'tags' model has a fk like message = models.ForeignKey(Message...)
    tags= django_filters.<some filter>(name='tags')

Fast expandable and clean to customize. note that I did not write this module, I am just very happy with it :)

0
source

Source: https://habr.com/ru/post/1762174/


All Articles