Commit 144d5013 authored by Gabriel G. Garrero's avatar Gabriel G. Garrero

Added get_absolute_url() functions to models

parent 10e37df4
from django.db import models from django.db import models
from django.urls import reverse
class Author(models.Model): class Author(models.Model):
first_name = models.CharField(max_length = 50) first_name = models.CharField(max_length = 50)
...@@ -10,6 +11,9 @@ class Author(models.Model): ...@@ -10,6 +11,9 @@ class Author(models.Model):
def __str__(self): def __str__(self):
return self.first_name + ' ' + self.last_name return self.first_name + ' ' + self.last_name
def get_absolute_url(self):
return reverse("author-detail", kwargs = {"pk": self.pk})
class Books(models.Model): class Books(models.Model):
title = models.CharField(max_length = 100) title = models.CharField(max_length = 100)
author = models.ForeignKey(Author, on_delete = models.CASCADE) author = models.ForeignKey(Author, on_delete = models.CASCADE)
...@@ -20,3 +24,6 @@ class Books(models.Model): ...@@ -20,3 +24,6 @@ class Books(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def get_absolute_url(self):
return reverse("books-detail", kwargs = {"pk": self.pk})
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment