如果不创build表单,我可以在Django中创build一个不需要的pipe理域吗?

每当我在Django的Admin部分input一个新玩家时,我都会得到一个错误消息,说“这个字段是必需的”。

有没有办法让一个字段不需要,而不必创build一个自定义表单? 我可以在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" ) 

谢谢

只要放

 blank=True 

在你的模型即:

 rushing_attempts = models.CharField( max_length = 100, verbose_name = "Rushing Attempts", blank=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 )