Opened 2 years ago

Closed 2 years ago

#33964 closed Bug (worksforme)

get_FOO_display() doesn't work with field named that include underscore

Reported by: Viktor Ovsiannikov Owned by: nobody
Component: Database layer (models, ORM) Version: 4.1
Severity: Normal Keywords: get_FOO_display() model
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If you try to use a get_FOO_display method on a field that contains underscore, it apparently breaks the syntax and the method doesn't work

Change History (1)

comment:1 by Tim Graham, 2 years ago

Resolution: worksforme
Status: newclosed

The following test passes for me. Please provide details to reproduce.

  • tests/model_regress/models.py

    diff --git a/tests/model_regress/models.py b/tests/model_regress/models.py
    index 350850393a..369eada93f 100644
    a b class Article(models.Model):  
    1111    status = models.IntegerField(blank=True, null=True, choices=CHOICES)
    1212    misc_data = models.CharField(max_length=100, blank=True)
    1313    article_text = models.TextField()
     14    the_status = models.IntegerField(blank=True, null=True, choices=CHOICES)
    1415
    1516    class Meta:
    1617        ordering = ("pub_date", "headline")
  • tests/model_regress/tests.py

    diff --git a/tests/model_regress/tests.py b/tests/model_regress/tests.py
    index 10dfbabcd9..cd7c6f3c48 100644
    a b class ModelTests(TestCase):  
    5656        # NOTE: Part of the regression test here is merely parsing the model
    5757        # declaration. The verbose_name, in particular, did not always work.
    5858        a = Article.objects.create(
    59             headline="Look at me!", pub_date=datetime.datetime.now()
     59            headline="Look at me!", pub_date=datetime.datetime.now(), the_status=1,
    6060        )
     61        self.assertIs(a.get_the_status_display(), 'first')
    6162        # An empty choice field should return None for the display name.
    6263        self.assertIs(a.get_status_display(), None)
Note: See TracTickets for help on using tickets.
Back to Top