[Fixed]-Get all form objects in a Django application

1๐Ÿ‘

โœ…

I found a way to use reflection style code to retrieve all of the objects inheriting from the models.Form class, see test below:

class TestAllFormsMeetBasicBoundaryTests(TestCase):
    def all_subclasses(self, cls):
        return cls.__subclasses__() + [g for s in cls.__subclasses__()
                                   for g in self.all_subclasses(s)]

    # this is teh awesome-sause! dynamically finding objects in the project FTW!
    def test_all_char_fields_contain_max_length_value(self):
        from django.apps import apps
        import django.forms
        import importlib
        log.info('loading all forms modules from apps')
        at_least_one_module_found = False
        for app_name in settings.PROJECT_APPS: #this is a list of in project apps
            app = apps.get_app_config(app_name)
            try:
                importlib.import_module('{0}.forms'.format(app.label))
                log.info('forms loaded from {0}'.format(app.label))
                at_least_one_module_found = True
            except ImportError as e:
                pass
        self.assertTrue(at_least_one_module_found, 'No forms modules were found in any of the apps. this should never be true!')
        all_forms = self.all_subclasses(django.forms.Form)

        messages = []
        for form in all_forms:
            for key in form.declared_fields.keys():
                field = form.declared_fields[key]
                if isinstance(field, django.forms.CharField) and form.__module__.partition('.')[0] in settings.PROJECT_APPS and field.max_length is None:
                    messages.append('{0}.{1}.{2} is missing a max_length value.'.format(form.__module__, form.__name__, key))
            pass

        self.assertEquals(0, len(messages),
                          'CharFields must always have a max_length attribute specified. please correct the following:\n--{0}'
                          .format('\n--'.join(messages)))

Leave a comment