【问题标题】:Using if else in django models.py在 django models.py 中使用 if else
【发布时间】:2020-08-05 11:56:40
【问题描述】:

我正在尝试在我的django models.py中创建管理界面,以便在选择状态变量完成时应要求各种字段。

class Event(models.Model):
    poster = models.ImageField(upload_to='EventPosters')
    title = models.CharField(max_length=200)
    description = models.TextField(max_length=350)
    STATUS = (('upcoming', 'upcoming'), ('completed', 'completed'))
    status = models.CharField(max_length=10, choices=STATUS, default='upcoming')
    if status == 'completed':
        report = models.TextField()
        images_url = models.URLField()
        no_of_images = models.CharField(max_length=1, validators=[DecimalValidator])
        images_list = {}
        for i in range(int(no_of_images)):
            images_list[i] = models.ImageField(upload_to='EventImages')

    def __str__(self):
        return self.title

上面是我要创建的模型的类,我想如果用户选择事件已完成,它应该自动在管理面板中询问剩余的字段。 提前感谢您的任何回答。

【问题讨论】:

  • models.py中不能这样做,如果要操作admin,调整admin.py

标签: python django web django-models django-admin


【解决方案1】:

我会在没有条件的情况下编写模型中的所有字段,并向它们添加参数“blank=True”。 并将向模板添加条件。如果触发条件,则会出现其他字段。

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    相关资源
    最近更新 更多