简体   繁体   English

django.core.exceptions.FieldError:不能为论坛 model 表单指定“日期”,因为它是不可编辑的字段

[英]django.core.exceptions.FieldError: 'date' cannot be specified for Forum model form as it is a non-editable field

Here is my models.py这是我的models.py

# Create your models here.
class Forum(models.Model):
    publisher = models.CharField('Публикатор', max_length=50, default='Anonymous')
    topic = models.CharField('Название', max_length=50)
    text = models.TextField('Текст')
    date = models.DateField(auto_now_add=True)
    comment = models.CharField('Комментарий', max_length=100, default='Комментарий')
    # slug = models.SlugField(max_length=200, unique=True, default='default')

    class Meta:
        verbose_name = 'Пост'
        verbose_name_plural = 'Посты'

    def __str__(self):
        return self.topic

How to solve such problem, I also tried set editable=True.如何解决这样的问题,我也试过set editable=True。 It didn't help.它没有帮助。

The problem is with问题在于

date = models.DateField(auto_now_add=True)

Here, you set it to auto_now_add=True , it will add current date time automatically but cannot edit later.在这里,您将其设置为auto_now_add=True ,它将自动添加当前日期时间,但以后无法编辑。 Try it to replace with尝试将其替换为

from django.utils import timezone

date = models.DateTimeField(default=timezone.now)

The following is from doc 1以下来自文档1

DateField.auto_now_add¶ Automatically set the field to now when the object is first created. DateField.auto_now_add¶ 首次创建 object 时自动将该字段设置为现在。 Useful for creation of timestamps.用于创建时间戳。 Note that the current date is always used;请注意,始终使用当前日期; it's not just a default value that you can override.它不仅仅是您可以覆盖的默认值。 So even if you set a value for this field when creating the object, it will be ignored.因此即使在创建 object 时为该字段设置了值,它也会被忽略。 If you want to be able to modify this field, set the following instead of auto_now_add=True:如果您希望能够修改此字段,请设置以下内容而不是 auto_now_add=True:

For DateField: default=date.today - from datetime.date.today() For DateTimeField: default=timezone.now - from django.utils.timezone.now()对于 DateField:default=date.today - 从 datetime.date.today() 对于 DateTimeField: default=timezone.now - 从 django.utils.timezone.now()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django.core.exceptions.FieldError:为 Post 指定的未知字段(模型) - django.core.exceptions.FieldError: Unknown field(s) (model) specified for Post django.core.exceptions.FieldError:无法将关键字'timestamp'解析为字段 - django.core.exceptions.FieldError: Cannot resolve keyword 'timestamp' into field 无法为文章模型表单指定“内容”,因为它是不可编辑的字段 - 'content' cannot be specified for Article model form as it is a non-editable field raise FieldError("Cannot resolve keyword '%s' into field." django.core.exceptions.FieldError: 无法解析关键字 - raise FieldError("Cannot resolve keyword '%s' into field. " django.core.exceptions.FieldError: Cannot resolve keyword Django:无法为订单模型表单指定“已创建”,因为它是不可编辑的字段 - Django: 'created' cannot be specified for Order model form as it is a non-editable field django.core.exceptions.FieldError:为用户指定的未知字段(出生日期) - django.core.exceptions.FieldError: Unknown field(s) (dateofbirth) specified for User django.core.exceptions.FieldError:为配置文件指定了未知字段(电子邮件) - django.core.exceptions.FieldError: Unknown field(s) (email) specified for Profile django.core.exceptions.FieldError:为员工指定的未知字段(用户名) - django.core.exceptions.FieldError: Unknown field(s) (username) specified for Employee django.core.exceptions.FieldError:为客户指定的未知字段(标题) - django.core.exceptions.FieldError: Unknown field(s) (title) specified for Customer django.core.exceptions.FieldError:为用户指定的未知字段(电话) - django.core.exceptions.FieldError: Unknown field(s) (phone) specified for User
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM