如果不创建表单,我可以在Django中创建一个不需要的管理字段吗?

时间:2011-09-07 22:05:55

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

每当我在Django的Admin部分中输入一个新玩家时,我会收到一条错误消息,上面写着“此字段是必需的。”。

有没有办法在不创建自定义表单的情况下使字段不需要?我可以在models.py或admin.py中执行此操作吗?

这是我在models.py中的类看起来的样子。

class PlayerStat(models.Model):
    player = models.ForeignKey(Player)

    rushing_attempts = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Attempts"
        )
    rushing_yards = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Yards"
        )
    rushing_touchdowns = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Touchdowns"
        )
    passing_attempts = models.CharField(
        max_length = 100,
        verbose_name = "Passing Attempts"
        )

谢谢

2 个答案:

答案 0 :(得分:120)

Just Put

blank=True

在你的模型中,即:

rushing_attempts = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Attempts",
        blank=True
        )

答案 1 :(得分:4)

使用blank = True,null = True

class PlayerStat(models.Model):
    player = models.ForeignKey(Player)

    rushing_attempts = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Attempts",
        blank=True,
        null=True
        )
    rushing_yards = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Yards",
        blank=True,
        null=True
        )
    rushing_touchdowns = models.CharField(
        max_length = 100,
        verbose_name = "Rushing Touchdowns",
        blank=True,
        null=True
        )
    passing_attempts = models.CharField(
        max_length = 100,
        verbose_name = "Passing Attempts",
        blank=True,
        null=True
        )