我可以在不创建表单的情况下创建 Django 中不需要的管理字段吗?
每次我在 Django 的管理部分输入新玩家时,我都会收到一条错误消息,显示“此字段是必填的。”。
有没有一种方法可以使字段成为不需要的字段,而无需创建自定义表单?我可以在 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"
)
谢谢
Every time I enter in a new player in the Admin portion of Django I get an error message that says "This field is required.".
Is there a way to make a field not required without having to create a custom form? Can I do this within models.py or admin.py?
Here's what my class in models.py looks like.
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"
)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需输入
您的模型即:
Just Put
in your model i.e.:
使用空白=True,null=True
Use blank=True, null=True
如果该字段在模型级别设置为空白,则实际上意味着允许空字符串。空字符串和 null 实际上不是同一件事。不要仅仅因为框架设置了一些不好的默认功能而破坏数据完整性。
不要设置空白,而是重写 get_form 方法:
If the field is set blankable in model level, it really means empty string are allowed. An empty string and a null really aren't the same thing. Don't break your data integrity only because the framework has set some bad default features.
Instead of setting the blank, override the get_form -method: