Reverse url lookup for Part model

This commit is contained in:
Oliver Walters 2019-04-25 00:59:34 +10:00
parent f9db3b680d
commit 0bc5617825
3 changed files with 23 additions and 4 deletions

View File

@ -89,7 +89,7 @@ class Part(models.Model):
"""
def get_absolute_url(self):
return '/part/{id}/'.format(id=self.id)
return reverse('part-detail', kwargs={'pk': self.id})
# Short name of the part
name = models.CharField(max_length=100, unique=True, help_text='Part name (must be unique)')

View File

@ -0,0 +1,22 @@
from django.test import TestCase
from .models import Part, PartCategory
class SimplePartTest(TestCase):
def setUp(self):
cat = PartCategory.objects.create(name='TLC', description='Top level category')
self.px = Part.objects.create(name='x', description='A part called x', buildable=True)
self.py = Part.objects.create(name='y', description='A part called y', consumable=False)
self.pz = Part.objects.create(name='z', description='A part called z', category=cat)
def test_metadata(self):
self.assertEqual(self.px.name, 'x')
self.assertEqual(self.py.get_absolute_url(), '/part/2/')
self.assertEqual(str(self.pz), 'z - A part called z')
def test_category(self):
self.assertEqual(self.px.category_path, '')
self.assertEqual(self.pz.category_path, 'TLC')

View File

@ -1,3 +0,0 @@
# from django.test import TestCase
# Create your tests here.