加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

django.core.exceptions.FieldError: 'pub_date&#39

发布时间:2020-12-15 17:14:30 所属栏目:大数据 来源:网络整理
导读:在django开发中,使用django form时提示错误: ??File?"D:softwarepython3.6libsite-packagesdjangoformsmodels.py",?line?256,?in?__new__????apply_limit_choices_to=False,??File?"D:softwarepython3.6libsite-packagesdjangoformsmodels.p

在django开发中,使用django form时提示错误:

??File?"D:softwarepython3.6libsite-packagesdjangoformsmodels.py",?line?256,?in?__new__
????apply_limit_choices_to=False,??File?"D:softwarepython3.6libsite-packagesdjangoformsmodels.py",?line?149,?in?fields_for_model
????f.name,?model.__name__)
django.core.exceptions.FieldError:?'pub_date'?cannot?be?specified?for?Article?model?form?as?it?is?a?non-editable?fiel

model.py

class?Article(TimeModel):
???
????title?=?models.CharField(max_length=150,?verbose_name='文档标题')
????writer?=?models.CharField(max_length=20,?verbose_name='作者')
????user?=?models.ForeignKey(User,?on_delete=models.CASCADE,?verbose_name='创建人')
????category?=?models.ForeignKey(Category,?db_index=True,?verbose_name='分类')
????arc_type?=?models.SmallIntegerField(default=0,?choices=TYPE,?verbose_name='文章类型')
????sort_rank?=?models.SmallIntegerField(default=0,?verbose_name='文档排序')
????status?=?models.SmallIntegerField(default=STATUS_NORMAL,?choices=STATUS_ITEMS,?verbose_name='文章状态')
????pub_date?=?models.DateTimeField(auto_now_add=True,?verbose_name='发布时间')

经过阅读下面文章,发现如果DateTimeField设置了auto_now_add=True导致,即使设置了editable=True,也是默认不支持修改。? ?

注意:

选项auto_now_add,auto_now和default互相排斥。这些选项的任何组合都将导致错误。

当前实现,设置auto_now或auto_now_add以 True会导致该领域拥有editable=False和blank=True 设置。

https://groups.google.com/forum/#!topic/django-users/74h8uPPiVoQ

auto_now_add官方文档:

https://docs.djangoproject.com/zh-hans/2.2/ref/models/fields/#django.db.models.DateField.auto_now_add

解决办法:

class?Article(TimeModel):
???
????title?=?models.CharField(max_length=150,?verbose_name='文章状态')
????pub_date?=?models.DateTimeField(verbose_name='发布时间')


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读