如何从该字典创建模型?

2024-04-26 01:32:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让表单输入用户从下面的字典,但不知道如何使它在django

    my_play = dict(
      name="may_first_play",
      hosts='cisco_firewalls',
      become='yes',
      become_method='enable',
      gather_facts='no',
      tasks=[
          dict(action=dict(module='asa_command', commands=['show version','show ip'])),
          dict(action=dict(module='asa_config', lines=['network-object host 10.1.0.1'])
      ]
  )

我曾制作过这样的模型,但在现场制作任务上令人困惑

    class Book(models.Model):
     name = models.CharField(max_length=100)
     hosts = models.CharField(max_length=100)
     become = models.CharField(max_length=100)
     become_method = models.CharField(max_length=100)
     gather_facts = models.CharField(max_length=100)

     class task(Book):`

如果你能帮助我,我将非常感激


1条回答
网友
1楼 · 发布于 2024-04-26 01:32:55

请尝试以下代码

您还需要在模型手册中添加“任务”字段

class Book(models.Model):
     name = models.CharField(max_length=100)
     hosts = models.CharField(max_length=100)
     become = models.CharField(max_length=100)
     become_method = models.CharField(max_length=100)
     gather_facts = models.CharField(max_length=100)
     tasks = models.TextField()

my_play = dict(
        name="may_first_play",
        hosts='cisco_firewalls',
        become='yes',
        become_method='enable',
        gather_facts='no',
        tasks=[
            dict(action=dict(module='asa_command', commands=['show version', 'show ip'])),
            dict(action=dict(module='asa_config', lines=['network-object host 10.1.0.1']))
        ]
    )

    book = Book(**my_play)
    book.save()

相关问题 更多 >