开发者

Django - Model property 'getter'?

开发者 https://www.devze.com 2023-02-05 12:55 出处:网络
If I had a model like this: class Entry(models.Model): STATUS_LIVE = 1 STATUS_DRAFT = 2 STATUS_HIDDEN = 3

If I had a model like this:

  class Entry(models.Model):
 STATUS_LIVE = 1
 STATUS_DRAFT = 2
 STATUS_HIDDEN = 3
 STATUS_CHOICES = (
  (STATUS_LIVE, ('Live')),
  (STATUS_DRAFT, ('Draft')),
  (STATUS_HIDDEN, ('Hidden')),
 )

 id = models.AutoField(primary_key=True)
 status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, 

And I wanted to show the property 'status' in a view as either 'Live', 'Draft' or 'Hidden' (as opposed to 1, 2 or 3), how would I do this?

It seems Django doesn't have any kind of getter / setter functionality inside of models, I'开发者_运维百科d like to be able to use entry.status and not something like entry.full_status because it becomes awfully confusing having lots of differently named properties.

Any suggestions would be appreciated


You would call Entry.get_status_display().

0

精彩评论

暂无评论...
验证码 换一张
取 消