本文介绍了在Django 1.7中不再可以使用数字键选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将其实现为模型的选择变量:

I tried to implement this as a choices variable for a model:

>>> choices = (
...         (1, 'Primary'), (2, 'Secondary'), (3, 'Tertiary')
...         )

但是它失败了,并出现了这种准密码错误:

But it failed with this quasi-cryptic error:

characters.CharacterArcanumLink.priority: (fields.E005) 'choices' must
be an iterable containing (actual value, human readable name) tuples.

让我们看看

  1. 选择是可迭代的吗?检查.
  2. 包含(实际值,人类可读的名称)元组?检查:实际值"是一个整数,人类可读的名称"是一个字符串.

看起来还可以.

进入代码,似乎对Django 1.7进行了新检查:

Digging into the code, it looks like new checks are done for Django 1.7:

def _check_choices(self):
        if self.choices:
            if (isinstance(self.choices, six.string_types) or
                    not is_iterable(self.choices)):
                return [
                    checks.Error(
                        "'choices' must be an iterable (e.g., a list or tuple).",
                        hint=None,
                        obj=self,
                        id='fields.E004',
                    )
                ]
            elif any(isinstance(choice, six.string_types) or
                     not is_iterable(choice) or len(choice) != 2
                     for choice in self.choices):
                return [
                    checks.Error(
                        ("'choices' must be an iterable containing "
                         "(actual value, human readable name) tuples."),
                        hint=None,
                        obj=self,
                        id='fields.E005',
                    )
                ]
            else:
                return []
        else:
            return []

它是第二个检查,仅次于看起来可疑的小精灵.让我们在上面的 choices 元组上尝试 isinstance :

Its the second check, after the elif that looks fishy. Lets try that isinstance on my choices tuple above:

>>> any(isinstance(choice, six.string_types) for choice in choices)
False

不好!进行一些修改怎么样?

No good! How about with some modification?

>>> any(isinstance(choice[1], six.string_types) for choice in choices)
True
>>>

优秀.

我的问题是,我错过了什么吗?我确定成为可能.为什么不再可能?我执行错误吗?

My question is, have I missed something? I'm sure this used to be possible. Why is it no longer possible? Am I implementing this wrongly?

我还在code.djangoproject上打开了门票,如果有帮助?

I've also opened a ticket on code.djangoproject, if that helps?

触发它的代码分为两个部分:

The code that trigger it comes in two parts:

class Trait(models.Model):
    PRIORITY_CHOICES = (
        (None, '')
        )
    MIN = 0
    MAX = 5
    current_value = IntegerRangeField(min_value=MIN, max_value=MAX)
    maximum_value = IntegerRangeField(min_value=MIN, max_value=MAX)
    priority = models.PositiveSmallIntegerField(choices=PRIORITY_CHOICES, default=None)
    class Meta:
        abstract = True

并且:

class CharacterSkillLink(CharacterLink, SkillLink, Trait):
    PRIORITY_CHOICES = (
        (1, 'Primary'), (2, 'Secondary'), (3, 'Tertiary')
        )
    speciality = models.CharField(max_length=200)

推荐答案

问题是当您有一个选择时-外括号不构成元组,仅提供分组即可.元组的创建由逗号而不是方括号提供.

The issue is when you have a single choice - the outer brackets don't construct a tuple, just provide grouping. The creation of a tuple is provided by the comma, not the brackets.

((None, '')) == (None, '')

您应该使用逗号结尾来表示您想要一个单项元组(或使用列表):

You should leave a trailing comma to signify that you want a single-item tuple (or use a list):

PRIORITY_CHOICES = ((None, ''), )

这篇关于在Django 1.7中不再可以使用数字键选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 18:52
查看更多