-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d282497
commit b1d623f
Showing
6 changed files
with
42 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,4 +136,3 @@ | |
SonarDjangoJsonResponseType, | ||
], | ||
) | ||
# Import and add Codemod class to registry above. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/core_codemods/docs/pixee_python_django-model-without-dunder-str.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
If you've ever actively developed or debugged a Django application, you may have noticed Django models and their instances can sometimes be hard to read or distinguish one instance from another. Loading models in the interactive Django console or viewing them in the admin interface can be puzzling. This is because Django is trying to display your model objects as a plain strings. | ||
|
||
We've written this codemod to make your model objects human-readable. It will automatically detect all of your model's fields and display them as a nice string. | ||
|
||
For example, the `Question` model from Django's popular Poll App tutorial will look like this: | ||
```diff | ||
from django.db import models | ||
|
||
class Question(models.Model): | ||
question_text = models.CharField(max_length=200) | ||
pub_date = models.DateTimeField("date published") | ||
+ | ||
+ def __str__(self): | ||
+ model_name = self.__class__.__name__ | ||
+ fields_str = ", ".join([f"{field.name}={getattr(self, field.name)}" for field in self._meta.fields]) | ||
+ return f"{model_name}({fields_str})" | ||
``` | ||
|
||
Without this change, the `Question` objects look like this in the interactive Django shell: | ||
``` | ||
>>> Question.objects.all() | ||
<QuerySet [<Question: Question object (1)>]> | ||
``` | ||
With this codemod's addition of `__str__`, it now looks like: | ||
``` | ||
>>> Question.objects.all() | ||
<QuerySet [<Question: Question(id=1, question_text=What's new?, pub_date=2024-02-21 14:28:45.631782+00:00)>]> | ||
``` | ||
|
||
You'll notice this change works great for models with only a handful of fields. We encourage you to use this codemod's change as a starting point for further customization. |
2 changes: 1 addition & 1 deletion
2
src/core_codemods/docs/pixee_python_django-session-cookie-secure-off.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters