comment model not attached to ticket model

Creating a separate comments app for ticket app. My issue is that when I create and submit my comment on a particle ticket, it creates its own new page of tickets instead of being attached to original ticket. I think the issue has to do with my urls.py file but i’m not sure how to proceed.

Here is my models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
from tickets.models import Ticket

class Comment(models.Model):
    ticket = models.ForeignKey(Ticket, related_name='comments', on_delete=models.CASCADE, null=True)
    title = models.CharField(max_length=20)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    
    def __str__(self):
        return self.title
    
    def get_absolute_url(self):
        return reverse('tickets:ticket-detail', kwargs={'pk': self.pk})
from django.db import models

from django.utils import timezone

from django.contrib.auth.models import User

from django.urls import reverse

from users.models import Profile

class Ticket(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    assignee = models.ForeignKey(Profile, on_delete=models.SET_NULL, blank=True, null=True)
    status = models.BooleanField(choices=MARKED, default=True)
    priority = models.TextField(choices=PRIORITIES, default='None', max_length=10)
    label = models.CharField(choices=TYPES, default='Misc', max_length=100)
    
    
    def __str__(self):
        return self.title
    
    def get_absolute_url(self):
        return reverse('ticket-detail', kwargs={'pk': self.pk})
    

main urls.py

from django.conf import settings
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static

app_name = 'tickets'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('users.urls')),
    path('', include(('tickets.urls', 'tickets'), namespace='tickets')),
    path('', include('tickets.urls')),
    path('', include('comments.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

tickets urls.py

from django.conf import settings
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static

app_name = 'tickets'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('users.urls')),
    path('', include(('tickets.urls', 'tickets'), namespace='tickets')),
    path('', include('tickets.urls')),
    path('', include('comments.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

comments urls.py

from django.urls import include, path
from .views import (
    CommentListView,
    CommentCreateView,
    CommentUpdateView,
    CommentDeleteView,
)

urlpatterns = [
    path('tickets/<int:pk>/', CommentListView.as_view(), name='ticket-detail'),
    path('tickets/<int:pk>/comments/new/', CommentCreateView.as_view(), name='comment-create'),
    path('tickets/comments/<int:pk>/update/', CommentUpdateView.as_view(), name='comment-update'),
    path('tickets/comments/<int:pk>/delete/', CommentDeleteView.as_view(), name='comment-delete'),
]

The get_absolute_url method is intended to return a url for the object to which it’s attached. In other words, it’s typically used to a detail page for itself. If that were true in this case, you would want it to reverse comment-update and not ticket-detail.

Now, I can envision a situation where you’d want it to return the url for the ticket page. (I would assume it would work properly, but there may be some unintended side effects.) If that’s the case and you really want to use it to reverse ticket-detail, then you need to pass the ticket’s pk to the reverse function, not the comment’s pk.

Would it be something like this?

def get_absolute_url(self):
     return reverse('tickets:ticket-detail', kwargs={'pk': self.ticket_id})
1 Like