rspec/rules/S6554/python/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

53 lines
1.4 KiB
Plaintext

This rule ensures that Django models have a `__str__` method defined.
== Why is this an issue?
The `__str__` method in Django models is used to represent the model instance as a string. For example, the return value of this method will be inserted in a template when displaying an object in the Django admin site. Without this method, the model instance will be represented by its object identifier, which is not meaningful to end-users. This can result in confusion and make debugging more difficult.
== How to fix it
To fix this issue, the Django model must define a `__str__` method that returns a string representation of the instance. This string should be meaningful to end-users and provide information about the model instance.
=== Code examples
==== Noncompliant code example
[source,python]
----
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
----
==== Compliant solution
[source,python]
----
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
----
== Resources
=== Documentation
https://docs.djangoproject.com/en/4.1/ref/models/instances/#django.db.models.Model.__str__[Django Model.__str__()]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Define a "__str__" method for this Django model.
'''
endif::env-github,rspecator-view[]