Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

python 3.x - How to do checks on Django form classes

I'm playing with django forms a little, and trying to check if all form fields correspond to a field of a model through name-checking, this is being done in form constructor function, but I find silly to make such a check on every form instance made so, I would like to do so but on form class. I came to think that to attain it I would need models to be loaded first in django and maybe... to listen to a signal and wait for executing the code once they are loaded, i read through Django Signal doc, but i'm not sure if code will fulfill per my purpose. Is there a way to do it??. Any help is welcome.

What you'll see below on the code section is a form with a basic filtering functionality.


class NoticeFilter(forms.ModelForm):
    name = forms.CharField(max_length=12, required=False)

    def checkfields(self):
        #Check that the form fields are a subset of the model fields
        #Granting that every field defined here corresponds only to a model field 
        #and has its same name, no aditional input.

        list_ = []
        field_names = set(boundfield.name for boundfield in self)
        for model_field in self.__class__.Meta.model._meta.get_fields():
        list_ = set(list_)
        if field_names.issubset(list_):
            print('You are good to go')
            print('You have one or more fields whose name doesnt match with any of the corresponding model fields.')
    def __init__(self,  *args, **kwargs):

        #check function, raises an error if put before __init__
        #Pop a queryset provided for filtering purposes through form field input
        queryset = kwargs.pop('queryset', None)
        super().__init__(*args, **kwargs)
        if not queryset:
            self.queryset = self.__class__.Meta.model.objects.all()
            self.queryset = queryset
        dict_ = {}

        if self.is_valid():
            #if form is valid then program can proceed to filter  

            for field in self:
                #make a dict from boundfields key, value pair, to pass it as kwargs 
                #to queryset filter method
                dict_.setdefault(field.name, field.value())

                self.qs = self.queryset.filter(**dict_)

            except FieldError:
                raise FieldError('You have inserted a set of fields which differ in name according to the names of the model fields you are using for this form.')

    class Meta:
        model = Notice
        fields = (
        widgets = {
            'tag' : autocomplete.ModelSelect2Multiple(url='tag-autocomplete'),

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share