0

I am trying to test one of my application's model Book that has a slug field. I have a custom save function for it, like below.

models.py

class Book(models.Model):
    title = models.CharField(max_length=50)
    slug = models.SlugField(max_length=50, unique=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            slug = slugify(self.title, allow_unicode=True)

        return super(Book, self).save(*args, **kwargs)

To test this, I created the following test.py

class BookModelTests(TestCase):

    @classmethod
    def setUpTestData(cls):
        Book.objects.create(title="Book One")

    def test_get_absolute_url(self):
        book = Book.objects.get(pk=1)
        self.assertEquals(book.get_absolute_url(), '/books/book-one/')

But when I run the test, it fails with django.urls.exceptions.NoReverseMatch: Reverse for 'book-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['books/(?P<book_slug>[-a-zA-Z0-9_]+)/$'].

Traceback

Traceback (most recent call last):
  File "Bookman/bookman/bms/tests/test_models.py", line 14, in test_get_absolute_url
    self.assertEquals(book.get_absolute_url(), '/books/book-one/')
  File "Bookman/bookman/bms/models.py", line 26, in get_absolute_url
    return reverse('book-detail', args=[str(self.slug)])
  File "Bookman/venv/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "Bookman/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 677, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'book-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['books/(?P<book_slug>[-a-zA-Z0-9_]+)/$']

Why is slug empty? Why is the save method not called? What am I missing?

1 Answer 1

1

Unintended mistake in your save method. You should use self.slug to assign the value to the object's slug like:

def save(self, *args, **kwargs):
    if not self.slug:
        self.slug = slugify(self.title, allow_unicode=True)

    return super(Book, self).save(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! Not again! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.