Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#31313 closed Cleanup/optimization (fixed)

Error in Example in Model field reference > Field.choices > Enumeration types

Reported by: Lee Hopkins Owned by: Andrey Doroschenko
Component: Documentation Version: 3.0
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The example in the new Field.choices Enumeration types documentation has an error:

class Student(models.Model):

    class YearInSchool(models.TextChoices):
        FRESHMAN = 'FR', _('Freshman')
        SOPHOMORE = 'SO', _('Sophomore')
        JUNIOR = 'JR', _('Junior')
        SENIOR = 'SR', _('Senior')
        GRADUATE = 'GR', _('Graduate')

    year_in_school = models.CharField(
        max_length=2,
        choices=YearInSchool.choices,
        default=YearInSchool.FRESHMAN,
    )

    def is_upperclass(self):
        return self.year_in_school in {YearInSchool.JUNIOR, YearInSchool.SENIOR}

The method is_upperclass() attempts to access the YearInSchool class directly, but cannot. The method should be:

    def is_upperclass(self):
        return self.year_in_school in {self.YearInSchool.JUNIOR, self.YearInSchool.SENIOR}

or:

    def is_upperclass(self):
        return self.year_in_school in {Student.YearInSchool.JUNIOR, Student.YearInSchool.SENIOR}

https://docs.djangoproject.com/en/3.0/ref/models/fields/#enumeration-types

Change History (5)

comment:1 Changed 3 years ago by Simon Charette

Has patch: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:2 Changed 3 years ago by Mariusz Felisiak

Owner: changed from nobody to Andrey Doroschenko
Severity: NormalRelease blocker
Status: newassigned

comment:3 Changed 3 years ago by Mariusz Felisiak

Triage Stage: AcceptedReady for checkin

comment:4 Changed 3 years ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In f101681:

Fixed #31313 -- Fixed is_upperclass() example in enumeration types docs.

comment:5 Changed 3 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In 59ac25c9:

[3.0.x] Fixed #31313 -- Fixed is_upperclass() example in enumeration types docs.

Backport of f1016814d84b1423cfe0df85644c9870a6bc6b41 from master

Note: See TracTickets for help on using tickets.
Back to Top