python - 如何在没有管理验证的情况下制作 django 表单字段

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

我正在学习 django,目前我正在开发 django 1.9。 我制作了一个名为experience的模型,其中包含:

from __future__ import unicode_literals

from django.db import models


# Create your models here.

class Experience(models.Model):
    designation = models.CharField(max_length=250)
    department = models.CharField(max_length=250, null=True)
    present = models.BooleanField(default=False)
    joining_date = models.DateField()
    ending_date = models.DateField(null=True)

    def __unicode__(self):
        return self.designation

    def __str__(self):
        return self.designation

现在,当我去体验表格时,部门和结束日期必须是必填数据。但我希望 departmentending_date 将传递 null 值,并且不会以 admin/experience 形式对这两个属性引发任何验证错误。

如何做到这一点?请帮助我。

最佳答案

只需将其更改为 ending_date = models.DateField(null=True, Blank=True)

关于python - 如何在没有管理验证的情况下制作 django 表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954322/

相关文章:

python - 将 numpy.array 存储在 Pandas.DataFrame 的单元格中

Django save() 覆盖,最佳实践 : on model, 表单或 View ?

jquery - 如何使用 jQuery ajax 实时搜索 Django 中的模型?

python - 字典的嵌套列表

python - 带有 Jupyter Notebook : Shortcut for “run all” ? 的 Visual Studio 代码

python - 显示基于另一个表数据的表单元素

python - Django 装置 : Cannot assign data to textfield using json file

python - 带有pycharm 5的 docker

从遗留数据库加载数据后,Django ORM 无法自动设置模型的主键

python - 用 selenium 打开 Tor 浏览器