简体   繁体   中英

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

Here is my 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. 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. Try it to replace with

from django.utils import timezone

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

The following is from doc 1

DateField.auto_now_add¶ Automatically set the field to now when the object is first created. 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. If you want to be able to modify this field, set the following instead of auto_now_add=True:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM