如何在 django admin 中显示固定下拉菜单?

发布于 2024-09-05 22:29:40 字数 155 浏览 6 评论 0原文

我想在下拉列表中显示优先级信息。目前我正在使用整数字段来存储优先级,但我想显示高/中/低而不是让用户输入优先级。

一种近似的方法是使用存储 3 个元素的优先级数据库,1:高、2:中、3:低,但这似乎有点矫枉过正。

任何更简单的方法将不胜感激!

贾森

I would like to display priority information in a drop down. Currently i am using a integer field to store the priority, but i would like to display high/medium/low instead of letting user type in a priority.

A way to approximate this is to use a Priority database which stores 3 elements, 1:high, 2:medium, 3:low, but it seems like an overkill.

Any easier way would be much appreciated!

Jason

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

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

发布评论

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

评论(3

别低头,皇冠会掉 2024-09-12 22:29:40

您可以指定字段的选择 http://www.djangoproject.com/documentation/models/choices /

PRIORITY_CHOICES = ((1, 'High'),
                    (2, 'Medium'),
                    (3, 'Low'))

class MyModel(models.Model):
    priority = models.IntegerField(choices=PRIORITY_CHOICES)

You can specify choices for a field http://www.djangoproject.com/documentation/models/choices/.

PRIORITY_CHOICES = ((1, 'High'),
                    (2, 'Medium'),
                    (3, 'Low'))

class MyModel(models.Model):
    priority = models.IntegerField(choices=PRIORITY_CHOICES)
﹎☆浅夏丿初晴 2024-09-12 22:29:40

您可以这样编写模型:

from django.db import models

class Priority(models.Model):
    PRIORITY_LOW = 3
    PRIORITY_MEDIUM = 2
    PRIORITY_HIGH = 1

    PRIORITY_CHOICES = (
        (PRIORITY_LOW, 'low'),
        (PRIORITY_MEDIUM, 'medium'),
        (PRIORITY_HIGH, 'high'),
    )

    priority = models.IntegerField(choices=PRIORITY_CHOICES)

您可以从 文档 中阅读更多内容

You could write your model like this:

from django.db import models

class Priority(models.Model):
    PRIORITY_LOW = 3
    PRIORITY_MEDIUM = 2
    PRIORITY_HIGH = 1

    PRIORITY_CHOICES = (
        (PRIORITY_LOW, 'low'),
        (PRIORITY_MEDIUM, 'medium'),
        (PRIORITY_HIGH, 'high'),
    )

    priority = models.IntegerField(choices=PRIORITY_CHOICES)

You read more from the documentation

肤浅与狂妄 2024-09-12 22:29:40

您应该能够向模型元素添加选择

,因此:

class myModel(models.Model):
    mydata = models.CharField(max_length=4, choices= ((u'H', u'High',), (u'M', u'Medium'), (u'L', u'Low')))

将在数据库中存储 H、M、L,但显示高、中、低。管理员将具有选项属性的默认字段设置为下拉选择器

You should be able to add choices to the model element

So:

class myModel(models.Model):
    mydata = models.CharField(max_length=4, choices= ((u'H', u'High',), (u'M', u'Medium'), (u'L', u'Low')))

Would store H,M,L in the DB, but present High, Medium, Low. Admin defaults fields with the choices attribute to a drop down selector

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