[英]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.