django模型,使用来自另一个对象的值作为选择

时间:2016-05-11 12:41:47

标签: python django

我试图使用另一个对象的值作为选择,如下所示。因此,我将在影响字段中有一个预定义的维护类型集列表供您选择。当我尝试创建此模型时,我收到错误

[root@network-tools infternal]# python manage.py makemigrations maintenance
SystemCheckError: System check identified some issues:

ERRORS:
maintenance.Maintenance.Impact: (fields.E004) 'choices' must be an iterable (e.g., a list or tuple).

模型配置

class MaintenanceType(models.Model):
    Type = models.CharField(max_length=200)

    class Meta:
                verbose_name = "Planned Maintenance Types"
                verbose_name_plural = "Planned Maintenance Types"

class Maintenance(models.Model):
    Title = models.CharField(max_length=200)
    Impact = models.CharField(max_length=200, choices=MaintenanceType)
    Description = models.TextField()
    StartTime = models.DateTimeField
    EndTime = models.DateTimeField

    class Meta:
                verbose_name = "Planned IT Maintenance"
                verbose_name_plural = "Planned IT Maintenance"    

1 个答案:

答案 0 :(得分:2)

' ForeignKey' 是更好的解决方案。你也可以尝试下面的代码。

models.py

    appbundle.form.listener:
        class: AppBundle\EventListener\FormListener
        tags:
            - { name: kernel.event_subscriber }

forms.py

Impact = models.CharField(max_length=200)