Search code examples
djangodjango-modelsdjango-testing

Django model's save method is not called when creating objects in test


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?


Solution

  • 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)