python - Django 字段选择两个值之间的范围?

标签 python django django-models slider

如何使用 Django 模型实现范围选择器?

我想要完成的是从预定义列表(普通列表,而不是 Python 列表)中选择数据,

  1. 低于平均水平
  2. 平均
  3. 高于平均水平
  4. 平均水平。

添加学生时,

class Student(models.Model):
    name = models.CharField(max_length=20)
    class = models.CharField(max_length=1)
    grade = ?

The Poor 是基础值,我想要的是添加一个范围选择,当一个学生添加值 good 时,他必须获取 poor 中的所有值到,而添加为平均的学生只会获得平均的值。

一个实际的例子是 slider ,

是否有一个字段可以完成此任务?

谢谢。

最佳答案

查看 https://docs.djangoproject.com/en/1.8/ref/models/fields/#choices 上的一些示例

在你的情况下,它会是这样的:

CHOICES = (
    (1, 'poor'),
    (2, 'below average'),
    (3, 'average'),
    (4, 'above average'),
    (5, 'good'),
    (6, 'average')
)

class Student(models.Model):
    name = models.CharField(max_length=20)
    class = models.CharField(max_length=1)
    grade = models.IntegerField(choices=CHOICES)

我不确定你的代码是否正确,但这应该会给你一个想法。

关于python - Django 字段选择两个值之间的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32027970/

相关文章:

python - Python 中的 header 检查 (GAE)

python - 构建 Django 多对多关系

python - Python 中的正则表达式

python - 如何在 matplotlib 箱线图中标记四分位数?

Java 相当于 Matlab 的 `eps` 或 Numpy/Python 的 `spacing` 函数(浮点相对精度)

Python:code.interact(local=locals()) 其中 stdin/stdout 不可用

ajax - request.is_ajax() == 点击后退按钮时为真?

mysql - Django如何执行SQL语句并返回queryset?

python_2_unicode_compatible 错误

python - 复制模型实例并更新字段