Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django DateTimeField is not editable

I have a blog and I post many articles. Each article has a date of publication, and sometimes I have to edit this date. Everything running on the Django admin panel and I never encountered any 500 error since I decided to add the DateTimeField in my admin.py.

The issue is, Django cannot edit the DateTimeField and it returns :

Internal Server Error: /admin/wall/articles/62/change/

FieldError at /admin/wall/articles/62/change/ 'date' cannot be specified for Articles model form as it is a non-editable field. Check fields/fieldsets/exclude attributes of class ArticlesAdmin.

I don't understand why it doesn't work.

Models :

class Articles(models.Model):
    title = models.CharField(max_length=100, null=False, verbose_name="Titre")
    description = models.TextField(max_length=500, null=False, verbose_name="Description pour les partages sur les réseaux sociaux")
    subtitle = models.TextField(max_length=300, null=True, verbose_name="Sous-titre")
    text = RichTextUploadingField()
    tag = models.ManyToManyField(Tag, verbose_name="Tag")
    subcategory = models.ForeignKey(SubCategory, verbose_name="Sous-catégorie", blank=True, null=True)
    image = models.FileField(upload_to='media/articles/', validators=[validate_file_extension], blank=True, null=True, verbose_name="Image de présentation")
    image_description = models.CharField(max_length=100, null=True, verbose_name="Description pour cette image")
    image_legende = models.CharField(max_length=100, null=True, verbose_name="Légende pour cette images")
    author = models.ForeignKey(User, verbose_name="Auteur")
    published = models.BooleanField(default=True, verbose_name="Publié")
    date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de création")
    update = models.DateTimeField(auto_now=True, verbose_name="Dernière modification")


    def get_absolute_url(self):
        return reverse('read', kwargs={'post':self.id, 'slug':slugify(self.title)})

    def __str__(self):
        return self.title

admin.py

class ArticlesAdmin(admin.ModelAdmin):
    list_display = ('date', 'title', 'author', 'published', 'update')

    fieldsets = (
        ('Général', {
            'fields': ('title', 'author', 'published', 'tag')
            }),
        ('Date de publication', {
            'fields': ('date',)}),
        ('Choisir une image de présentation (700px de largeure et 320px de hauteur', {
            'fields': ('image',)}),
        ('Description de l\'image', {
            'fields': ('image_description',)}),
        ('Légende de l\'image', {
            'fields': ('image_legende',)}),
        ('Sous-titre de l\'article', {
            'fields': ('subtitle',)}),                  
        ('Contenu de l\'article', {
            'fields': ('text', 'description')}),
    )

admin.site.register(Articles, ArticlesAdmin)
like image 330
GrandGTO Avatar asked Dec 07 '22 13:12

GrandGTO


1 Answers

auto_now_add=True makes that field not editable. You should disable auto_now_add (auto_now_add=False) and implement that behaviour yourself (for example, in save() method of Articles self.date = timezone.now()).

Little remark about naming: you should name your model in single form, not plural (Article instead of Articles)

like image 59
Baydin Konstantin Avatar answered Mar 08 '23 09:03

Baydin Konstantin