在enumchoicefield中使用Django管理界面

时间:2018-11-26 23:56:00

标签: django django-models

我正在尝试将Django(2.1.3)管理界面与enumchoicefield软件包一起使用。创建和执行迁移以及启动Django一切都很好,但是当我尝试向包含EnumChoiceField的模型添加实例时,我得到了:

  

异常类型:TypeError
  异常值:render()获得了意外的关键字参数'renderer'
  异常位置:as_widget中第93行的/home/django/Env/rosella/lib/python3.5/site-packages/django/forms/boundfield.py
  Python可执行文件:/ usr / local / bin / uwsgi   Python版本:3.5.2

型号代码:

from enumchoicefield import ChoiceEnum, EnumChoiceField
...
class SystemStatus(ChoiceEnum):
    UNKNOWN = 'Unknown'
    OK = 'Ok'
    DOWN = 'Down'

class Monitor(models.Model):
    ...
    status = EnumChoiceField(SystemStatus, default=SystemStatus.UNKNOWN)

问题:enumchoicefield是否支持管理界面? 注意:我尝试使用django_enumfield进行枚举,但是也遇到了管理界面问题-错误为'EnumType' object is not iterable

1 个答案:

答案 0 :(得分:0)

I am not sure if you should use EnumChoiceField, as the library does not have any information about supports in Django 2.X. Also it has mentioned that it is using Python 3.4 in github page. Instead, consider choice fields like this:

class SystemStatus(object):
    UNKNOWN = 'Unknown'
    OK = 'Ok'
    DOWN = 'Down'
    choices = (
     (UNKNOWN, "Unknown"),
     (OK, 'Ok'),
     (DOWN, 'Down')
    )

And use it in Model:

status = models.CharField(max_length=20, choices=SystemStatus.choices, default=SystemStatus.UNKNOWN)