Home > Net >  "<Post:>" needs to have a value for field "id" before this many-to-many re
"<Post:>" needs to have a value for field "id" before this many-to-many re

Time:02-24

When i'm trying to add a Post through django admin i get an error that the Post im trying to add needs to have a value for field id. Do you have any idea why?

now = datetime.now()

class Category(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)

    class Meta:
        verbose_name_plural = "categories"

    def __str__(self):
        return self.name


class Post(models.Model):
    title = models.CharField(max_length=100)
    excerpt = models.CharField(max_length=200)
    main_image = models.ImageField()
    author = models.ForeignKey(users.models.CustomUser, on_delete=models.CASCADE, related_name='blog_posts', null=True)
    content = models.TextField(null=True)
    created_at = models.DateTimeField(editable=False)
    updated_at = models.DateTimeField(editable=False)
    category = models.ManyToManyField(Category, related_name='post_category')

    class Meta:
        ordering = ['-created_at']

    def save(self, *args, **kwargs):
        if not self.id:
            self.created_at = now
        self.updated_at = now

    def __str__(self):
        return self.title

CodePudding user response:

You need to make a super().save(*args, **kwargs) call. Furthermore using a constant will not work: this will assign the time when you started the server, not the current time, so:

from django.utils.timezone import now

class Post(models.Model):
    # …

    def save(self, *args, **kwargs):
        if not self.id:
            self.created_at = now()
        self.updated_at = now()
        super().save(*args, **kwargs)

You furthermore do not need to specify logic to update the created_at and updated_at field, you can work with auto_now_add=True [Django-doc] and auto_now=True [Django-doc]:

class Post(models.Model):
    # …
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    # …

    class Meta:
        ordering = ['-created_at']

    # no save override

    def __str__(self):
        return self.title
  • Related