Home > Back-end >  POSTGRESQL - DJANGO - time without time zone en date
POSTGRESQL - DJANGO - time without time zone en date

Time:08-21

I'm working on a project's Django with a Postegresql database.

I just created a model like that :

from django.db import models

from members.models import CustomUser


class Article(models.Model):
    title = models.CharField(max_length=250)
    body = models.TextField()
    custom_user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ["-created_at"]

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

When I try to migrate, I've this issue : ERREUR: can't convert type time without time zone en date

In my DB, I've in my table "time without time zone".

Do you have any ideas ? Thanks !

CodePudding user response:

Try using DateField instead of DateTimeField, DateField does not store timezone.

Read the documentation here: https://docs.djangoproject.com/en/4.1/ref/models/fields/#datefield

This might help:

from django.db import models

from members.models import CustomUser


class Article(models.Model):
    title = models.CharField(max_length=250)
    body = models.TextField()
    custom_user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    created_at = models.DateField(auto_now_add=True, blank=True, null=True) # --> changed to DateField
    updated_at = models.DateField(auto_now=True, blank=True, null=True) # --> changed to DateField

    def __str__(self):
        return self.title

    class Meta:
        ordering = ["-created_at"]

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

CodePudding user response:

DateField and DateTimeField return the same error. I deleted the database and recreate it.

  • Related