轻松处理选项字段
easy-choices的Python项目详细描述
这是一个深受django-model-utils选择启发的图书馆。 然而,有时我们只需要使用选项,而不是使用django-model-utils提供的所有功能。
要求
- python>;=3.5
用法
easy-choices包托管在我们的PyPI repository上。
您可以使用pip:
$ pip install easy-choices
使用简单的选择,如下所示:
fromdjango.dbimportmodelsfromeasy_choicesimportChoicesstatus_choices=Choices(("sent","Sent"),("delivered","Delivered"),)classProduct(models.Model)price=models.DecimalField(max_digits=10,decimal_places=2)status=models.CharField(max_length=10,choices=status_choices.to_django_choices())@propertydefis_delivered(self):# You can use status_choices as a Enumreturnself.status==status_choices.delivered