Django:显示选择值

发布于 2024-10-05 03:47:36 字数 941 浏览 0 评论 0原文

models.py:

class Person(models.Model):
    name = models.CharField(max_length=200)
    CATEGORY_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    gender = models.CharField(max_length=200, choices=CATEGORY_CHOICES)
    to_be_listed = models.BooleanField(default=True)
    description = models.CharField(max_length=20000, blank=True)

views.py:

def index(request):
    latest_person_list2 = Person.objects.filter(to_be_listed=True)
    return object_list(request, template_name='polls/schol.html',
                       queryset=latest_person_list, paginate_by=5)

在模板上,当我调用 person.gender 时,我得到 'M' 或 'F' 而不是 'Male''Female'

如何显示值('Male''Female')而不是代码('M'/'F' )?

models.py:

class Person(models.Model):
    name = models.CharField(max_length=200)
    CATEGORY_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    gender = models.CharField(max_length=200, choices=CATEGORY_CHOICES)
    to_be_listed = models.BooleanField(default=True)
    description = models.CharField(max_length=20000, blank=True)

views.py:

def index(request):
    latest_person_list2 = Person.objects.filter(to_be_listed=True)
    return object_list(request, template_name='polls/schol.html',
                       queryset=latest_person_list, paginate_by=5)

On the template, when I call person.gender, I get 'M' or 'F' instead of 'Male' or 'Female'.

How to display the value ('Male' or 'Female') instead of the code ('M'/'F')?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自由范儿 2024-10-12 03:47:36

看来您走在正确的轨道上 - get_FOO_display() 肯定是您想要的:

模板中,名称中不包含()的一个方法。执行以下操作:

{{ person.get_gender_display }}

It looks like you were on the right track - get_FOO_display() is most certainly what you want:

In templates, you don't include () in the name of a method. Do the following:

{{ person.get_gender_display }}
冰雪梦之恋 2024-10-12 03:47:36

对于每个设置了选项的字段,该对象将有一个 get_FOO_display() 方法,其中 FOO 是字段的名称。此方法返回字段的“人类可读”值。

在视图中

person = Person.objects.filter(to_be_listed=True)
context['gender'] = person.get_gender_display()

在模板中

{{ person.get_gender_display }}

get_FOO_display() 的文档

For every field that has choices set, the object will have a get_FOO_display() method, where FOO is the name of the field. This method returns the “human-readable” value of the field.

In Views

person = Person.objects.filter(to_be_listed=True)
context['gender'] = person.get_gender_display()

In Template

{{ person.get_gender_display }}

Documentation of get_FOO_display()

白云悠悠 2024-10-12 03:47:36

我通过以下方式定义常量来实现此目的:

class Person(models.Model):
    MALE = 'M'
    FEMALE = 'F'
    CATEGORY_CHOICES = (
        (MALE, 'Male'),
        (FEMALE, 'Female'),
    )

    name = models.CharField(max_length=200)
    gender = models.CharField(max_length=200, choices=CATEGORY_CHOICES)
    to_be_listed = models.BooleanField(default=True)
    description = models.CharField(max_length=20000, blank=True)

M 和 F 值将存储在数据库中,而 Male 和 Female 值将显示在各处。

I do this by defining constants the following way:

class Person(models.Model):
    MALE = 'M'
    FEMALE = 'F'
    CATEGORY_CHOICES = (
        (MALE, 'Male'),
        (FEMALE, 'Female'),
    )

    name = models.CharField(max_length=200)
    gender = models.CharField(max_length=200, choices=CATEGORY_CHOICES)
    to_be_listed = models.BooleanField(default=True)
    description = models.CharField(max_length=20000, blank=True)

The M and F values will be stored in the database, while the Male and Female values will display everywhere.

煮茶煮酒煮时光 2024-10-12 03:47:36

其他人指出 get_FOO_display 方法正是您所需要的。
我正在使用这个:

def get_type(self):
    return [i[1] for i in Item._meta.get_field('type').choices if i[0] == self.type][0]

它迭代特定项目的所有选择,直到找到与项目类型匹配的选项

Others have pointed out that a get_FOO_display method is what you need.
I'm using this:

def get_type(self):
    return [i[1] for i in Item._meta.get_field('type').choices if i[0] == self.type][0]

which iterates over all of the choices that a particular item has until it finds the one that matches the items type

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文