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

ruff: Two flake8-django issues to be fixed #489

Open
cclauss opened this issue Jul 27, 2023 · 0 comments
Open

ruff: Two flake8-django issues to be fixed #489

cclauss opened this issue Jul 27, 2023 · 0 comments

Comments

@cclauss
Copy link
Contributor

cclauss commented Jul 27, 2023

Good First Issue

% pipx install ruff
% ruff --select=DJ --show-source .

test_app/models.py:12:7: DJ008 Model does not define `__str__` method
   |
12 | class Company(models.Model):
   |       ^^^^^^^ DJ008
13 |     name = models.CharField(
14 |         max_length=100,
   |

waffle/models.py:107:5: DJ012 Order of model's inner classes, methods, and fields does not follow
    the Django Style Guide: `save` method should come before custom method
    |
105 |           cache.delete_many(keys)
106 |
107 |       def save(self, *args: Any, **kwargs: Any) -> None:
    |  _____^
108 | |         self.modified = timezone.now()
109 | |         ret = super().save(*args, **kwargs)
110 | |         if hasattr(transaction, 'on_commit'):
111 | |             transaction.on_commit(self.flush)
112 | |         else:
113 | |             self.flush()
114 | |         return ret
    | |__________________^ DJ012
115 |
116 |       def delete(self, *args: Any, **kwargs: Any) -> tuple[int, dict[str, int]]:
    |

Found 2 errors.

% 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}"

% ruff rule DJ012

Derived from the flake8-django linter.

What it does

Checks for the order of Model's inner classes, methods, and fields as per
the [Django Style Guide].

Why is this bad?

The [Django Style Guide] specifies that the order of Model inner classes,
attributes and methods should be as follows:

  1. All database fields
  2. Custom manager attributes
  3. class Meta
  4. def __str__()
  5. def save()
  6. def get_absolute_url()
  7. Any custom methods

Examples

from django.db import models


class StrBeforeFieldModel(models.Model):
    class Meta:
        verbose_name = "test"
        verbose_name_plural = "tests"

    def __str__(self):
        return "foobar"

    first_name = models.CharField(max_length=32)
    last_name = models.CharField(max_length=40)

Use instead:

from django.db import models


class StrBeforeFieldModel(models.Model):
    first_name = models.CharField(max_length=32)
    last_name = models.CharField(max_length=40)

    class Meta:
        verbose_name = "test"
        verbose_name_plural = "tests"

    def __str__(self):
        return "foobar"
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

No branches or pull requests

1 participant