django/django

View on GitHub
django/forms/models.py

Summary

Maintainability
F
1 wk
Test Coverage

File models.py has 1026 lines of code (exceeds 250 allowed). Consider refactoring.
Open

"""
Helper functions for creating Form classes from Django models
and database field objects.
"""

Severity: Major
Found in django/forms/models.py - About 2 days to fix

    Function validate_unique has a Cognitive Complexity of 49 (exceeds 5 allowed). Consider refactoring.
    Open

        def validate_unique(self):
            # Collect unique_checks and date_checks to run from all the forms.
            all_unique_checks = set()
            all_date_checks = set()
            forms_to_delete = self.deleted_forms
    Severity: Minor
    Found in django/forms/models.py - About 7 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function fields_for_model has a Cognitive Complexity of 40 (exceeds 5 allowed). Consider refactoring.
    Open

    def fields_for_model(model, fields=None, exclude=None, widgets=None,
                         formfield_callback=None, localized_fields=None,
                         labels=None, help_texts=None, error_messages=None,
                         field_classes=None, *, apply_limit_choices_to=True):
        """
    Severity: Minor
    Found in django/forms/models.py - About 6 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function add_fields has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring.
    Open

        def add_fields(self, form, index):
            """Add a hidden field for the object's primary key."""
            from django.db.models import AutoField, OneToOneField, ForeignKey
            self._pk_field = pk = self.model._meta.pk
            # If a pk isn't editable, then it won't be on the form, so we need to
    Severity: Minor
    Found in django/forms/models.py - About 3 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function inlineformset_factory has 21 arguments (exceeds 4 allowed). Consider refactoring.
    Open

    def inlineformset_factory(parent_model, model, form=ModelForm,
    Severity: Major
    Found in django/forms/models.py - About 2 hrs to fix

      Function _get_foreign_key has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
      Open

      def _get_foreign_key(parent_model, model, fk_name=None, can_fail=False):
          """
          Find and return the ForeignKey from model to parent if there is one
          (return None if can_fail is True and no such field exists). If fk_name is
          provided, assume it is the name of the ForeignKey field. Unless can_fail is
      Severity: Minor
      Found in django/forms/models.py - About 2 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function modelformset_factory has 19 arguments (exceeds 4 allowed). Consider refactoring.
      Open

      def modelformset_factory(model, form=ModelForm, formfield_callback=None,
      Severity: Major
      Found in django/forms/models.py - About 2 hrs to fix

        Function construct_instance has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
        Open

        def construct_instance(form, instance, fields=None, exclude=None):
            """
            Construct and return a model instance from the bound ``form``'s
            ``cleaned_data``, but do not save the returned instance to the database.
            """
        Severity: Minor
        Found in django/forms/models.py - About 2 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function __new__ has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
        Open

            def __new__(mcs, name, bases, attrs):
                base_formfield_callback = None
                for b in bases:
                    if hasattr(b, 'Meta') and hasattr(b.Meta, 'formfield_callback'):
                        base_formfield_callback = b.Meta.formfield_callback
        Severity: Minor
        Found in django/forms/models.py - About 2 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function _construct_form has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
        Open

            def _construct_form(self, i, **kwargs):
                pk_required = i < self.initial_form_count()
                if pk_required:
                    if self.is_bound:
                        pk_key = '%s-%s' % (self.add_prefix(i), self.model._meta.pk.name)
        Severity: Minor
        Found in django/forms/models.py - About 2 hrs to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function _update_errors has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
        Open

            def _update_errors(self, errors):
                # Override any validation error messages defined at the model level
                # with those defined at the form level.
                opts = self._meta
        
        
        Severity: Minor
        Found in django/forms/models.py - About 1 hr to fix

        Cognitive Complexity

        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

        A method's cognitive complexity is based on a few simple rules:

        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
        • Code is considered more complex for each "break in the linear flow of the code"
        • Code is considered more complex when "flow breaking structures are nested"

        Further reading

        Function fields_for_model has 12 arguments (exceeds 4 allowed). Consider refactoring.
        Open

        def fields_for_model(model, fields=None, exclude=None, widgets=None,
        Severity: Major
        Found in django/forms/models.py - About 1 hr to fix

          Function modelform_factory has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
          Open

          def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
                                formfield_callback=None, widgets=None, localized_fields=None,
                                labels=None, help_texts=None, error_messages=None,
                                field_classes=None):
              """
          Severity: Minor
          Found in django/forms/models.py - About 1 hr to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Function modelform_factory has 11 arguments (exceeds 4 allowed). Consider refactoring.
          Open

          def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
          Severity: Major
          Found in django/forms/models.py - About 1 hr to fix

            Function __init__ has 11 arguments (exceeds 4 allowed). Consider refactoring.
            Open

                def __init__(self, queryset, *, empty_label="---------",
            Severity: Major
            Found in django/forms/models.py - About 1 hr to fix

              Function __init__ has 11 arguments (exceeds 4 allowed). Consider refactoring.
              Open

                  def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
              Severity: Major
              Found in django/forms/models.py - About 1 hr to fix

                Function save_existing_objects has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                Open

                    def save_existing_objects(self, commit=True):
                        self.changed_objects = []
                        self.deleted_objects = []
                        if not self.initial_forms:
                            return []
                Severity: Minor
                Found in django/forms/models.py - About 1 hr to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Function _get_validation_exclusions has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                Open

                    def _get_validation_exclusions(self):
                        """
                        For backwards-compatibility, exclude several types of fields from model
                        validation. See tickets #12507, #12521, #12553.
                        """
                Severity: Minor
                Found in django/forms/models.py - About 1 hr to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Function __init__ has 8 arguments (exceeds 4 allowed). Consider refactoring.
                Open

                    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
                Severity: Major
                Found in django/forms/models.py - About 1 hr to fix

                  Function _save_m2m has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                  Open

                      def _save_m2m(self):
                          """
                          Save the many-to-many fields and generic relations for this form.
                          """
                          cleaned_data = self.cleaned_data
                  Severity: Minor
                  Found in django/forms/models.py - About 55 mins to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Function add_fields has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
                  Open

                      def add_fields(self, form, index):
                          super().add_fields(form, index)
                          if self._pk_field == self.fk:
                              name = self._pk_field.name
                              kwargs = {'pk_field': True}
                  Severity: Minor
                  Found in django/forms/models.py - About 55 mins to fix

                  Cognitive Complexity

                  Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                  A method's cognitive complexity is based on a few simple rules:

                  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                  • Code is considered more complex for each "break in the linear flow of the code"
                  • Code is considered more complex when "flow breaking structures are nested"

                  Further reading

                  Function __init__ has 7 arguments (exceeds 4 allowed). Consider refactoring.
                  Open

                      def __init__(self, data=None, files=None, instance=None,
                  Severity: Major
                  Found in django/forms/models.py - About 50 mins to fix

                    Avoid deeply nested control flow statements.
                    Open

                                            for field in unique_check:
                                                if field in form.cleaned_data:
                                                    del form.cleaned_data[field]
                                        # mark the data as seen
                                        seen_data.add(row_data)
                    Severity: Major
                    Found in django/forms/models.py - About 45 mins to fix

                      Function __init__ has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                      Open

                          def __init__(self, data=None, files=None, instance=None,
                                       save_as_new=False, prefix=None, queryset=None, **kwargs):
                              if instance is None:
                                  self.instance = self.fk.remote_field.model()
                              else:
                      Severity: Minor
                      Found in django/forms/models.py - About 45 mins to fix

                      Cognitive Complexity

                      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                      A method's cognitive complexity is based on a few simple rules:

                      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                      • Code is considered more complex for each "break in the linear flow of the code"
                      • Code is considered more complex when "flow breaking structures are nested"

                      Further reading

                      Function _check_values has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                      Open

                          def _check_values(self, value):
                              """
                              Given a list of possible PK values, return a QuerySet of the
                              corresponding objects. Raise a ValidationError if a given value is
                              invalid (not a valid PK, not in the queryset, etc.)
                      Severity: Minor
                      Found in django/forms/models.py - About 45 mins to fix

                      Cognitive Complexity

                      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                      A method's cognitive complexity is based on a few simple rules:

                      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                      • Code is considered more complex for each "break in the linear flow of the code"
                      • Code is considered more complex when "flow breaking structures are nested"

                      Further reading

                      Function __init__ has 5 arguments (exceeds 4 allowed). Consider refactoring.
                      Open

                          def __init__(self, parent_instance, *args, pk_field=False, to_field=None, **kwargs):
                      Severity: Minor
                      Found in django/forms/models.py - About 35 mins to fix

                        Function model_to_dict has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                        Open

                        def model_to_dict(instance, fields=None, exclude=None):
                            """
                            Return a dict containing the data in ``instance`` suitable for passing as
                            a Form's ``initial`` keyword argument.
                        
                        
                        Severity: Minor
                        Found in django/forms/models.py - About 35 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Function save_new_objects has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                        Open

                            def save_new_objects(self, commit=True):
                                self.new_objects = []
                                for form in self.extra_forms:
                                    if not form.has_changed():
                                        continue
                        Severity: Minor
                        Found in django/forms/models.py - About 35 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Function _post_clean has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                        Open

                            def _post_clean(self):
                                opts = self._meta
                        
                                exclude = self._get_validation_exclusions()
                        
                        
                        Severity: Minor
                        Found in django/forms/models.py - About 25 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Function get_queryset has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                        Open

                            def get_queryset(self):
                                if not hasattr(self, '_queryset'):
                                    if self.queryset is not None:
                                        qs = self.queryset
                                    else:
                        Severity: Minor
                        Found in django/forms/models.py - About 25 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Function _construct_form has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                        Open

                            def _construct_form(self, i, **kwargs):
                                form = super()._construct_form(i, **kwargs)
                                if self.save_as_new:
                                    mutable = getattr(form.data, '_mutable', None)
                                    # Allow modifying an immutable QueryDict.
                        Severity: Minor
                        Found in django/forms/models.py - About 25 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Function clean has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                        Open

                            def clean(self, value):
                                if value in self.empty_values:
                                    if self.pk_field:
                                        return None
                                    # if there is no value act as we did before.
                        Severity: Minor
                        Found in django/forms/models.py - About 25 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        There are no issues that match your filters.

                        Category
                        Status