# comment model not attached to ticket model

**URL:** <https://forum.djangoproject.com/t/comment-model-not-attached-to-ticket-model/14836>\
**Category:** Forms & APIs\
**Created:** [July 16, 2022, 3:34am UTC](https://forum.djangoproject.com/t/comment-model-not-attached-to-ticket-model/14836 "2022-07-16T03:34:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mdaw11](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Mdaw11](https://forum.djangoproject.com/u/Mdaw11)\
**Post date:** [July 16, 2022, 3:34am UTC](https://forum.djangoproject.com/t/comment-model-not-attached-to-ticket-model/14836/1 "2022-07-16T03:34:55Z")

</div>

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

```auto
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})

```

```auto
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

```auto
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

```auto
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

```auto
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'),
]

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 16, 2022, 11:00am UTC](https://forum.djangoproject.com/t/comment-model-not-attached-to-ticket-model/14836/2 "2022-07-16T11:00:04Z")

</div>

> [@Mdaw11](#):
>
> ```auto
> def get_absolute_url(self):
> return reverse('tickets:ticket-detail', kwargs={'pk': self.pk})
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![Mdaw11](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Mdaw11](https://forum.djangoproject.com/u/Mdaw11)\
**Post date:** [July 26, 2022, 6:57pm UTC](https://forum.djangoproject.com/t/comment-model-not-attached-to-ticket-model/14836/3 "2022-07-26T18:57:37Z")

</div>

Would it be something like this?

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

```
