I was wondering what the convention was for line length when inputting help_text and other hard-coded long lines into Python/Django. I have read PEP-8, where line length is covered for code and comments, however I am not sure how this applies for long strings of text.
This is the for the field 'explanation_text', and the help_text field option.
class Question(models.Model):
questionnaire = models.ForeignKey(Questionnaire, on_delete=models.CASCADE)
title = models.CharField(max_length=150, blank=False)
category = models.CharField(max_length=20, blank=False)
created_date = models.DateTimeField(default=datetime.now, blank=True)
explanation_text = models.TextField(
blank=True,
help_text="Explanation text goes here. Candidates will be able to see this after they have taken a questionnaire. To change this, refer to the setting on questionnaire administration. Max length is 1000 characters.",
max_length=1000)
def __str__(self):
return self.title
You could store the help_text
string as a multi-line string using triple-quotation marks as follows:
help_text = """Explanation text goes here. Candidates will be able to see
this after they have taken a questionnaire. To change this,
refer to the setting on questionnaire administration. Max
length is 1000 characters."""
However, it might be more conventional to either:
store the multi-line string in a constant at the top of your models.py
file:
HELP_TEXT = """Explanation text.....
..................
"""
class Question(...):
...
help_text = HELP_TEXT
group all of your constants together in a constants.py
file. In models.py
you would then have:
import constants
class Question(...):
...
help_text = constants.HELP_TEXT
As maazza said, there is no convention.
As far as I'm concerned I like to use python implicit string concatenation.
class Question(models.Model):
explanation_text = models.TextField(
blank=True,
help_text=(
"Explanation text goes here. Candidates will be able to see "
"this after they have taken a questionnaire. To change this, "
"refer to the setting on questionnaire administration. "
"Max length is 1000 characters."),
max_length=1000)
Which works cleanly when using gettext:
from django.utils.translation import gettext_lazy as _
class Question(models.Model):
explanation_text = models.TextField(
blank=True,
help_text=_(
"Explanation text goes here. Candidates will be able to see "
"this after they have taken a questionnaire. To change this, "
"refer to the setting on questionnaire administration. "
"Max length is 1000 characters."),
max_length=1000)
Note: By the way, this his how django do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With