Skip to content

Post.get_absolute_url() references non-existent slug field, causing AttributeError #855

@vjpixel

Description

@vjpixel

Description

The Post.get_absolute_url() method in src/blog/models.py references self.slug, but the Post model has no slug field defined. Calling this method on any Post instance raises an AttributeError.

Location

File: src/blog/models.py
Class: Post
Method: get_absolute_url()
Branch: develop

Current Code

class Post(TimeStampedModel):
    id = models.BigAutoField(primary_key=True)
    title = models.CharField(max_length=200)
    status = models.CharField(...)
    author = models.ForeignKey(...)
    body = models.TextField()
    categories = models.ManyToManyField(...)
    images = models.ManyToManyField(...)
    # No 'slug' field!

    def get_absolute_url(self):
        return f"/memories/{self.slug}/"  # AttributeError: 'Post' object has no attribute 'slug'

Problem

The Post model does not define a slug field, but get_absolute_url() uses self.slug. Any code (templates, admin, sitemaps) that calls post.get_absolute_url() will raise:

AttributeError: 'Post' object has no attribute 'slug'

Suggested Fix

Either add a slug field to the Post model:

slug = models.SlugField(unique=True, max_length=200)

Or change get_absolute_url() to use the id field:

def get_absolute_url(self):
    return f"/memories/{self.id}/"

Severity

High — Any call to get_absolute_url() on a Post instance raises AttributeError.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CMSbugSomething isn't workingpriority: highHigh priority - should be addressed soonpriority: mediumMedium priority - standard priority

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions