Django不接受空白字段是有原因的吗?

时间:2018-10-05 22:02:41

标签: python django sqlite

我正在做一个项目,最初我希望一些必填字段现在是可选的。但是,如果将较新的字段留为空白,它们仍然会引发错误。

从我的models.py:

class PlanOfAction(models.Model):
PoA_1 = models.CharField(max_length=255, blank=True, default="")
q1_why = models.CharField(max_length=255, blank=True, default="")
PoA_2 = models.CharField(max_length=255, blank=True, default="yeah")
q2_why = models.CharField(max_length=255, blank=True, default="")
PoA_3 = models.CharField(max_length=255, blank=True, default="")
q3_why = models.CharField(max_length=255, blank=True, default="")

long_term_goal = models.CharField(max_length=255, blank=True, default="")
long_term_date = models.CharField(max_length=255, blank=True, default="")
long_term_actions_1 = models.CharField(max_length=255, blank=True, default="")
long_term_actions_2 = models.CharField(max_length=255, blank=True, default="")
long_term_actions_3 = models.CharField(max_length=255, blank=True, default="")

short_term_1 = models.CharField(max_length=255, blank=True, default="")
short_term_1_date = models.CharField(max_length=255, blank=True, default="")
short_term_actions_1_1 = models.CharField(max_length=255, blank=True, default="")
short_term_actions_1_2 = models.CharField(max_length=255, blank=True, default="")
short_term_actions_1_3 = models.CharField(max_length=255, blank=True, default="")

created_by = models.ForeignKey(User, related_name="user_poa")

objects = PlanManager()

created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)

它在models.py中写到数据库的位置

 new_plan = PlanOfAction.objects.create(
            PoA_1 = POST['PoA_1'],
            q1_why = POST['q1_why'],
            PoA_2 = POST['PoA_2'],
            q2_why = POST['q2_why'],
            PoA_3 = POST['PoA_3'],
            q3_why = POST['q3_why'],

            long_term_goal = POST['long_term_goal'],
            long_term_date = POST['long_term_date'],
            long_term_actions_1 = POST['long_term_actions_1'],
            long_term_actions_2 = POST['long_term_actions_2'],
            long_term_actions_3 = POST['long_term_actions_3'],

            short_term_1 = POST['short_term_goal_1'],
            short_term_1_date = POST['short_term_1_date'],
            short_term_actions_1_1 = POST['short_term_actions_1_1'],
            short_term_actions_1_2 = POST['short_term_actions_1_2'],
            short_term_actions_1_3 = POST['short_term_actions_1_3'],

            created_by = User.objects.get(id=id),
        )    

如果我将短期操作留空,则不会出错。我确实在以后添加了这些,并最初将它们设置为blank = True和default =“”。更新其他字段后,我删除了迁移文件夹db.sqlite3,运行migration,makemigrations,然后再次迁移。如果该字段为空,我每次都会在PoA_2 = POST ['PoA_2']上出错。

MultiValueDictKeyError at /view_your_plan
"u'PoA_2'"

有什么想法吗?

使用Python 2.7。

0 个答案:

没有答案