Skip to content

Count Django's models, forms, commands, template tags and receivers as used, and add rules for Django REST framework - #283

Merged
Apakottur merged 3 commits into
mainfrom
django-models-rest-framework
Nov 22, 2026
Merged

Apakottur merged 3 commits into
mainfrom
django-models-rest-framework

Conversation

@Apakottur

Copy link
Copy Markdown
Owner

Extends the django rules to models, forms, class-based views, management commands, template libraries, signal receivers and django-filter's filter sets, and adds a rest-framework framework for Django REST framework. The framework option is rest-framework, after the rest_framework package, as flask-restful is named. Its enum variant is Drf, since clippy rejects a variant that ends with the enum's name (Framework::RestFramework).

What was already covered

I first ran main on a sample project with Django 6.1, DRF 3.18 and django-filter 26.2 installed in the --python environment. external_members already counts as used what a base class outside the project defines and a subclass overrides: save, clean, Serializer.validate, ModelAdmin options such as list_display, the queryset/serializer_class/permission_classes of a generic view, and the actions of a ModelViewSet, which its mixins define.

It does not cover:

  • names that Django reads by convention without defining them: Meta, Media, fields, clean_<field>, validate_<field>, get_<field>;
  • a Manager's get_queryset: Manager's base is the call BaseManager.from_queryset(QuerySet), which the hierarchy cannot follow;
  • anything at all when Django is not installed.

The rules below cover the conventions. Where it is cheap, they also cover what external_members gives only with the environment, such as the attributes of APIView and of BaseCommand, so the rules also work without Django installed.

The rules

Each was checked against the source of the installed version (file in parentheses):

django

  • Models (subclasses of Model, or of AbstractUser/AbstractBaseUser when Django is not installed):
    • Fields: attributes set to a call of a class under django. whose name ends with Field, ForeignKey, ForeignObject, GenericRelation or CompositePrimaryKey, or of a subclass of one.
    • The default manager: the first attribute set to a Manager (or a subclass) or to QuerySet.as_manager(). Options.default_manager takes the first manager. Other managers are used only by name, so they are still reported.
    • The Meta class and its options. Options.contribute_to_class raises TypeError for any other attribute, so every attribute is an option.
    • get_absolute_url, which the admin, redirect() and the sitemaps call.
    • This follows the precedent of SQLAlchemy's mapped columns and Pydantic's fields.
  • Managers: get_queryset of subclasses of Manager.
  • Forms: fields (DeclarativeFieldsMetaclass) and clean_<field> methods (BaseForm._clean_fields). The Meta class of a ModelForm (ModelFormMetaclass).
  • Media of forms, widgets and the admin's ModelAdmin and inlines (MediaDefiningClass).
  • Class-based views: methods named by HTTP methods in subclasses of django.views.View or of a view of django.views.generic (View.dispatch).
  • Management commands:
    • The class Command of a module of a management/commands package whose name does not start with _ (find_commands, load_command_class), when it subclasses BaseCommand.
    • In any subclass of BaseCommand: handle, add_arguments, create_parser, handle_label, handle_app_config, and attributes such as help.
  • Template libraries:
    • Functions registered by filter, simple_tag, simple_block_tag, inclusion_tag or tag of a module-level Library(), through the Instances/registers helpers, so an unrelated x.filter does not match.
    • The module-level register variable set to a Library(), which import_library reads.
  • Receivers: functions decorated with django.dispatch.receiver.
  • django-filter: the filters (attributes set to a call of a class under django_filters ending with Filter) and the Meta of a FilterSet (FilterSetMetaclass, FilterSetOptions). It is a Django app, used both by Django views and by DRF, so I put it under django instead of adding a third framework.

rest-framework

  • Serializers (subclasses of a class under rest_framework ending with Serializer):
    • Fields: attributes set to a call of a class under rest_framework ending with Field or Serializer, which covers nested serializers (SerializerMetaclass._get_declared_fields).
    • The Meta class.
    • validate_<field> methods (Serializer.to_internal_value).
    • get_<field> when the serializer has an attribute <field> set to SerializerMethodField() (SerializerMethodField.bind).
  • Views (APIView, and classes of rest_framework.generics and rest_framework.viewsets):
    • Methods named by HTTP methods.
    • The attributes that APIView and GenericAPIView define, such as serializer_class, permission_classes and pagination_class.
    • The attributes that the filter backends read with getattr(view, ...): search_fields, ordering_fields and ordering (DRF), and filterset_class and filterset_fields (django-filter's DjangoFilterBackend). Also throttle_scope (ScopedRateThrottle).
  • Viewsets: list, create, retrieve, update, partial_update and destroy, the actions that the routers map. Methods with @action (get_extra_actions).

Base classes, decorators and calls resolve through the class hierarchy to the dotted names of the framework, so aliases, re-exports and project subclasses work, and look-alike names do not match. The test cases have negative lines for each rule.

Left out

  • SerializerMethodField(method_name="..."), @<action>.mapping.<method> and register.filter("name", func) written as a call.
  • The admin's options and methods without Django installed: they are many, and external_members covers them with the environment.

Findings

Binaries: the release binary of main (b785eca) against this branch. Projects: the Django projects of scripts/ecosystem.py (django, saleor and zulip), plus netbox, a well-known DRF and django-filter project, because none of the ecosystem projects uses DRF. The environment has django, djangorestframework and django-filter.

Project With environment Without environment
django 3919 → 2517 (−1403, +1) 3919 → 2517 (−1403, +1)
saleor 2776 → 2572 (−204) 2810 → 2606 (−204)
zulip 870 → 585 (−286, +1) 1417 → 1136 (−282, +1)
netbox 4639 → 3115 (−1524) 5669 → 4146 (−1523)

The +1s are not false positives. With --show-transitive, every project shows +0. They are unused attributes that were hidden as transitive, and are shown now that their class is used:

  • PartialDefNode.partial_name in django, a template tag node, set and never read;
  • self.strict and self.modified in two zulip commands, both set and never read.

The other 15 ecosystem projects are unchanged, byte for byte after sorting, with and without the environment, with --show-transitive and without. The run time is the same within noise.

What was removed, by cause, with the environment:

  • django: 1151 fields (mostly of models in tests/, and the forms of contrib), 128 template tags and filters, 49 commands, 25 receivers, 33 GenericRelation/ForeignObject fields, 10 managers, 9 clean_<field>, 7 Meta options.
  • saleor: 99 django-filter filters, 63 Meta options, 23 model fields, 19 commands.
  • zulip: 107 model and form fields, 100 commands, 66 Meta classes, 8 clean_<field>, 3 Meta options, 2 receivers.
  • netbox:
    • fields: 705 django-filter filters, 338 form fields, 201 serializer fields, 99 model fields;
    • methods: 36 clean_<field>, 22 get_<field>, 4 validate_<field>, 4 @action, 3 viewset actions;
    • 31 receivers, 12 template filters, 11 commands, 11 Meta options.

I went through a random sample of about 50 removed findings across the four projects, and the ones behind each cause listed above. Each is a name that Django, DRF or django-filter reads or calls.

Remaining Django false positives, not done here

Counted on the same runs, with the environment:

  • Classes named by a dotted path in settings, such as MIDDLEWARE, AUTHENTICATION_BACKENDS and storage backends: about 23 in zulip, 15 in saleor and 4 in netbox. These are classes that are only named in a string in a settings file, a rough count. This needs the settings' string values resolved to project classes.
  • Test classes whose base is a nested class, such as class X(ViewTestCases.PrimaryObjectViewTestCase): 231 in netbox. The class hierarchy does not follow a base class that is an attribute of a class. This is an engine limitation, not a Django rule.
  • Members that templates use, such as the methods of django.contrib.admin.helpers: 59 in django. They cannot be known from the Python code.
  • The columns and Meta of django-tables2 tables: about 290 findings under tables/ in netbox. A third-party package, a candidate for its own rules.
  • @admin.register: 3 in django, and none in the other projects.
  • A command whose base class comes from a package that is not installed, such as django_rq's Command: 1 in netbox, which is fine once that package is installed.
  • Not Django: graphene's class Arguments (198 in saleor) and the strawberry filters of netbox (about 660 under graphql/).

Verification

cargo test, uv run pytest -c tests/pytest.ini tests and uv run prek run -a pass. Tests: frameworks_django is extended (models, forms, filters, views, commands, template tags, signals), frameworks_rest_framework is new, and frameworks_selected turns rest-framework off and checks that nothing is counted as used.

…d receivers as used, and add rules for Django REST framework

Assisted-by: Claude:claude-opus-5-5
…amework

# Conflicts:
#	tests/_cli/test_config.py
Assisted-by: Claude:claude-opus-5-5
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 unref | 🛠️ Build #36072850 | 📁 Comparing 10ad8c8 against latest (74661dc)

  🔍 Preview build  

1 file changed
± frameworks/index.html

@github-actions

Copy link
Copy Markdown
Contributor

Performance

Time to check CPython v3.15.0: the median of 15 runs of each binary, run in turns.

Cores Base Head Change
Single core 1.55s 1.59s +2.5%
Multi core 1.04s 1.05s +0.9%

Ecosystem

Findings on 19 projects: 3 changed.

django: +1 -1403
- django/contrib/admin/helpers.py:32:5: unused-attribute 'select_across'
- django/contrib/admin/templatetags/admin_filters.py:14:5: unused-function 'to_object_display_value'
- django/contrib/admin/templatetags/admin_list.py:38:5: unused-function 'paginator_number'
- django/contrib/admin/templatetags/admin_list.py:77:5: unused-function 'pagination_tag'
- django/contrib/admin/templatetags/admin_list.py:368:5: unused-function 'result_list_tag'
- django/contrib/admin/templatetags/admin_list.py:489:5: unused-function 'date_hierarchy_tag'
- django/contrib/admin/templatetags/admin_list.py:514:5: unused-function 'search_form_tag'
- django/contrib/admin/templatetags/admin_list.py:526:5: unused-function 'admin_list_filter'
- django/contrib/admin/templatetags/admin_list.py:547:5: unused-function 'admin_actions_tag'
- django/contrib/admin/templatetags/admin_list.py:554:5: unused-function 'change_list_object_tools_tag'
- django/contrib/admin/templatetags/admin_modify.py:52:5: unused-function 'prepopulated_fields_js_tag'
- django/contrib/admin/templatetags/admin_modify.py:117:5: unused-function 'submit_row_tag'
- django/contrib/admin/templatetags/admin_modify.py:124:5: unused-function 'change_form_object_tools_tag'
- django/contrib/admin/templatetags/admin_modify.py:136:5: unused-function 'cell_count'
- django/contrib/admin/templatetags/admin_modify.py:156:5: unused-function 'admin_actions_tag'
- django/contrib/admin/templatetags/admin_urls.py:12:5: unused-function 'admin_urlname'
- django/contrib/admin/templatetags/admin_urls.py:17:5: unused-function 'admin_urlquote'
- django/contrib/admin/templatetags/log.py:27:5: unused-function 'get_admin_log'
- django/contrib/auth/forms.py:526:5: unused-attribute 'old_password'
- django/contrib/auth/hashers.py:141:5: unused-function 'reset_hashers'
- django/contrib/contenttypes/management/commands/remove_stale_contenttypes.py:10:7: unused-class 'Command'
- django/contrib/flatpages/forms.py:43:9: unused-method 'clean_url'
- django/contrib/flatpages/models.py:12:5: unused-attribute 'enable_comments'
- django/contrib/flatpages/templatetags/flatpages.py:47:5: unused-function 'get_flatpages'
- django/contrib/gis/db/backends/oracle/models.py:51:5: unused-attribute 'cs_name'
- django/contrib/gis/db/backends/oracle/models.py:58:5: unused-attribute 'cs_bounds'
- django/contrib/gis/db/backends/postgis/models.py:15:5: unused-attribute 'f_table_catalog'
- django/contrib/gis/db/backends/postgis/models.py:16:5: unused-attribute 'f_table_schema'
- django/contrib/gis/db/backends/spatialite/models.py:18:5: unused-attribute 'spatial_index_enabled'
- django/contrib/gis/db/backends/spatialite/models.py:60:5: unused-attribute 'ref_sys_name'
- django/contrib/gis/management/commands/inspectdb.py:4:7: unused-class 'Command'
- django/contrib/gis/management/commands/ogrinspect.py:35:7: unused-class 'Command'
- django/contrib/humanize/templatetags/humanize.py:24:5: unused-function 'ordinal'
- django/contrib/humanize/templatetags/humanize.py:73:5: unused-function 'intcomma'
- django/contrib/humanize/templatetags/humanize.py:131:5: unused-function 'intword'
- django/contrib/humanize/templatetags/humanize.py:158:5: unused-function 'apnumber'
- django/contrib/sessions/management/commands/clearsessions.py:7:7: unused-class 'Command'
- django/contrib/staticfiles/management/commands/findstatic.py:7:7: unused-class 'Command'
- django/core/management/commands/check.py:8:7: unused-class 'Command'
- django/core/management/commands/compilemessages.py:29:7: unused-class 'Command'
- django/core/management/commands/createcachetable.py:19:7: unused-class 'Command'
- django/core/management/commands/dbshell.py:7:7: unused-class 'Command'
- django/core/management/commands/diffsettings.py:9:7: unused-class 'Command'
- django/core/management/commands/dumpdata.py:30:7: unused-class 'Command'
- django/core/management/commands/flush.py:10:7: unused-class 'Command'
- django/core/management/commands/optimizemigration.py:15:7: unused-class 'Command'
- django/core/management/commands/sendtestemail.py:8:7: unused-class 'Command'
- django/core/management/commands/showmigrations.py:10:7: unused-class 'Command'
- django/core/management/commands/sqlflush.py:6:7: unused-class 'Command'
- django/core/management/commands/sqlmigrate.py:7:7: unused-class 'Command'
... and 1354 more
saleor: +0 -204
- saleor/account/i18n.py:65:9: unused-attribute 'labels'
- saleor/account/management/commands/createsuperuser.py:28:7: unused-class 'Command'
- saleor/account/models.py:201:5: unused-attribute 'search_document'
- saleor/account/models.py:383:5: unused-attribute 'is_public'
- saleor/account/models.py:384:5: unused-attribute 'customer'
- saleor/account/models.py:475:9: unused-attribute 'verbose_name_plural'
- saleor/app/management/commands/app_delete_all.py:11:7: unused-class 'Command'
- saleor/app/management/commands/app_list.py:12:7: unused-class 'Command'
- saleor/app/management/commands/app_set_active.py:13:7: unused-class 'Command'
- saleor/app/management/commands/create_app.py:18:7: unused-class 'Command'
- saleor/app/management/commands/install_app.py:16:7: unused-class 'Command'
- saleor/app/management/commands/webhook_set_active.py:11:7: unused-class 'Command'
- saleor/attribute/management/commands/update_attribute_file_urls.py:15:7: unused-class 'Command'
- saleor/attribute/models/base.py:32:9: unused-attribute 'abstract'
- saleor/attribute/models/base.py:179:5: unused-attribute 'page_types'
- saleor/attribute/models/base.py:186:5: unused-attribute 'customer_types'
- saleor/attribute/models/base.py:202:5: unused-attribute 'is_variant_only'
- saleor/attribute/models/base.py:235:9: unused-attribute 'unique_together'
- saleor/attribute/models/base.py:447:9: unused-attribute 'unique_together'
- saleor/attribute/models/base.py:536:9: unused-attribute 'unique_together'
- saleor/attribute/models/customer.py:25:9: unused-attribute 'unique_together'
- saleor/attribute/models/customer.py:44:9: unused-attribute 'unique_together'
- saleor/attribute/models/page.py:25:9: unused-attribute 'unique_together'
- saleor/attribute/models/page.py:44:9: unused-attribute 'unique_together'
- saleor/attribute/models/product.py:25:9: unused-attribute 'unique_together'
- saleor/attribute/models/product.py:46:9: unused-attribute 'unique_together'
- saleor/attribute/models/product_variant.py:28:9: unused-attribute 'unique_together'
- saleor/attribute/models/product_variant.py:52:9: unused-attribute 'unique_together'
- saleor/attribute/models/product_variant.py:62:5: unused-attribute 'assigned_variants'
- saleor/attribute/models/product_variant.py:74:9: unused-attribute 'unique_together'
- saleor/checkout/models.py:123:5: unused-attribute 'last_automatic_completion_attempt'
- saleor/core/management/commands/check_mptt_trees.py:110:7: unused-class 'Command'
- saleor/core/management/commands/clean_editorjs_fields.py:58:7: unused-class 'Command'
- saleor/core/management/commands/cleardb.py:26:7: unused-class 'Command'
- saleor/core/management/commands/clearorders.py:44:7: unused-class 'Command'
- saleor/core/management/commands/delete_event_payloads.py:6:7: unused-class 'Command'
- saleor/core/management/commands/generate_json_schemas.py:14:7: unused-class 'Command'
- saleor/core/management/commands/populatedb.py:34:7: unused-class 'Command'
- saleor/core/management/commands/remove_invalid_files.py:17:7: unused-class 'Command'
- saleor/core/management/commands/update_search_indexes.py:10:7: unused-class 'Command'
- saleor/core/models.py:20:9: unused-attribute 'abstract'
- saleor/core/models.py:76:9: unused-attribute 'abstract'
- saleor/core/models.py:99:9: unused-attribute 'abstract'
- saleor/core/models.py:144:9: unused-attribute 'abstract'
- saleor/core/models.py:156:9: unused-attribute 'abstract'
- saleor/core/utils/translations.py:34:9: unused-attribute 'abstract'
- saleor/csv/models.py:28:5: unused-attribute 'export_file'
- saleor/discount/models.py:308:9: unused-attribute 'unique_together'
- saleor/discount/models.py:326:9: unused-attribute 'unique_together'
- saleor/discount/models.py:337:9: unused-attribute 'unique_together'
... and 154 more
zulip: +1 -282
- analytics/management/commands/check_analytics_state.py:29:7: unused-class 'Command'
- analytics/management/commands/clear_analytics_tables.py:11:7: unused-class 'Command'
- analytics/management/commands/clear_single_stat.py:11:7: unused-class 'Command'
- analytics/management/commands/populate_analytics_db.py:34:7: unused-class 'Command'
- analytics/management/commands/update_analytics_counts.py:19:7: unused-class 'Command'
- analytics/models.py:43:11: unused-class 'Meta'
- analytics/models.py:48:11: unused-class 'Meta'
- analytics/models.py:71:11: unused-class 'Meta'
- analytics/models.py:101:11: unused-class 'Meta'
- analytics/models.py:133:11: unused-class 'Meta'
- confirmation/models.py:245:11: unused-class 'Meta'
- corporate/lib/stripe.py:710:5: unused-attribute 'website'
- corporate/lib/stripe.py:713:5: unused-attribute 'organization_type'
- corporate/management/commands/initialize_fixed_price_plan.py:15:7: unused-class 'Command'
- corporate/management/commands/link_customer_to_stripe_id.py:13:7: unused-class 'Command'
- corporate/models/customers.py:47:11: unused-class 'Meta'
- corporate/models/plans.py:18:11: unused-class 'Meta'
- corporate/models/plans.py:48:11: unused-class 'Meta'
- corporate/models/plans.py:198:11: unused-class 'Meta'
- corporate/models/sponsorships.py:20:5: unused-attribute 'requested_by'
- corporate/models/stripe_state.py:12:5: unused-attribute 'stripe_event_id'
- corporate/models/stripe_state.py:99:5: unused-attribute 'stripe_payment_intent_id'
- corporate/models/stripe_state.py:110:5: unused-attribute 'last_payment_error'
- corporate/views/support.py:85:5: unused-attribute 'request_subject'
- corporate/views/support.py:86:5: unused-attribute 'request_message'
- corporate/views/support.py:102:5: unused-attribute 'organization_name'
- corporate/views/support.py:103:5: unused-attribute 'organization_type'
- corporate/views/support.py:104:5: unused-attribute 'organization_website'
- corporate/views/support.py:105:5: unused-attribute 'expected_user_count'
- corporate/views/support.py:106:5: unused-attribute 'type_of_hosting'
- corporate/views/support.py:112:5: unused-attribute 'organization_website'
- corporate/views/support.py:113:5: unused-attribute 'expected_user_count'
- zerver/forms.py:128:5: unused-attribute 'realm_subdomain'
- zerver/forms.py:129:5: unused-attribute 'realm_type'
- zerver/forms.py:132:5: unused-attribute 'realm_default_language'
- zerver/forms.py:133:5: unused-attribute 'realm_name'
- zerver/forms.py:149:9: unused-method 'clean_realm_subdomain'
- zerver/forms.py:175:5: unused-attribute 'how_realm_creator_found_zulip'
- zerver/forms.py:178:5: unused-attribute 'how_realm_creator_found_zulip_other_text'
- zerver/forms.py:179:5: unused-attribute 'how_realm_creator_found_zulip_where_ad'
- zerver/forms.py:180:5: unused-attribute 'how_realm_creator_found_zulip_which_organization'
- zerver/forms.py:183:5: unused-attribute 'how_realm_creator_found_zulip_review_site'
- zerver/forms.py:184:5: unused-attribute 'how_realm_creator_found_zulip_which_ai_chatbot'
- zerver/forms.py:211:9: unused-method 'clean_full_name'
- zerver/forms.py:219:9: unused-method 'clean_password'
- zerver/forms.py:230:5: unused-attribute 'terms'
- zerver/forms.py:231:5: unused-attribute 'realm_type'
- zerver/forms.py:234:5: unused-attribute 'realm_default_language'
- zerver/forms.py:248:5: unused-attribute 'terms'
- zerver/forms.py:274:9: unused-method 'clean_email'
... and 233 more

@Apakottur
Apakottur merged commit d95ef25 into main Nov 22, 2026
8 checks passed
@Apakottur
Apakottur deleted the django-models-rest-framework branch November 22, 2026 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant