Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ruff --select=DJ #65

Open
cclauss opened this issue Feb 25, 2024 · 1 comment · May be fixed by #68
Open

Add support for ruff --select=DJ #65

cclauss opened this issue Feb 25, 2024 · 1 comment · May be fixed by #68

Comments

@cclauss
Copy link
Contributor

cclauss commented Feb 25, 2024

Motivation

% ruff --select=DJ # https://docs.astral.sh/ruff/rules/django-model-without-dunder-str

warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `pyproject.toml`:
  - 'ignore' -> 'lint.ignore'
  - 'select' -> 'lint.select'
tests/roots/test-docstrings/dummy_django_app/models.py:22:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:26:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:82:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:86:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:90:7: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:119:11: DJ008 Model does not define `__str__` method
tests/roots/test-docstrings/dummy_django_app/models.py:123:7: DJ008 Model does not define `__str__` method
Found 7 errors.

Proposed Solution

Edit pyproject.toml to fix the two warnings above and add DJ to the lint.select and then add .__str__() methods to the 7 Django models.

% ruff rule DJ008

django-model-without-dunder-str (DJ008)

Derived from the flake8-django linter.

What it does

Checks that __str__ method is defined in Django models.

Why is this bad?

Django models should define __str__ method to return a string representation
of the model instance, as Django calls this method to display the object in
the Django Admin and elsewhere.

Models without __str__ method will display a non-meaningful representation
of the object in the Django Admin.

Example

from django.db import models


class MyModel(models.Model):
    field = models.CharField(max_length=255)

Use instead:

from django.db import models


class MyModel(models.Model):
    field = models.CharField(max_length=255)

    def __str__(self):
        return f"{self.field}"

Alternatives

Additional Context

@timobrembeck
Copy link
Collaborator

@cclauss feel free to contribute this change, but I don't think it's strictly necessary. The dummy django app is only used for testing and not contained in the package distribution shipped via PyPI.

@cclauss cclauss linked a pull request Mar 3, 2024 that will close this issue
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 a pull request may close this issue.

2 participants