Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django help_text line length convention

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
like image 951
skydiver-uplift-besotted Avatar asked Sep 15 '25 22:09

skydiver-uplift-besotted


2 Answers

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
    
like image 71
gtlambert Avatar answered Sep 17 '25 10:09

gtlambert


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.

like image 35
Antoine Pinsard Avatar answered Sep 17 '25 11:09

Antoine Pinsard