Skip to content

Commit

Permalink
make Category.slug unique
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Sep 25, 2019
1 parent ddf1330 commit 83aeee0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Django Categories grew out of our need to provide a basic hierarchical taxonomy

As a news site, our stories, photos, and other content get divided into "sections" and we wanted all the apps to use the same set of sections. As our needs grew, the Django Categories grew in the functionality it gave to category handling within web pages.

=======
New in 2.0
==========

* Category.slug becomes unique. You must remove all duplicates in category slugs before running migrations.

New in 1.4
==========

Expand Down
2 changes: 1 addition & 1 deletion categories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CategoryBase(MPTTModel):
verbose_name=_('parent'),
)
name = models.CharField(max_length=100, verbose_name=_('name'))
slug = models.SlugField(verbose_name=_('slug'))
slug = models.SlugField(verbose_name=_('slug'), unique=True)
active = models.BooleanField(default=True, verbose_name=_('active'))

objects = CategoryManager()
Expand Down
2 changes: 1 addition & 1 deletion categories/fixtures/musicgenres.json
Original file line number Diff line number Diff line change
Expand Up @@ -2292,7 +2292,7 @@
"name": "Country pop",
"parent": 142,
"level": 1,
"slug": "country-pop",
"slug": "country-pop1",
"lft": 100,
"tree_id": 10,
"order": 1
Expand Down
18 changes: 18 additions & 0 deletions categories/migrations/0003_auto_20181005_1559.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.9 on 2018-10-05 13:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('categories', '0002_auto_20170217_1111'),
]

operations = [
migrations.AlterField(
model_name='category',
name='slug',
field=models.SlugField(unique=True, verbose_name='slug'),
),
]
2 changes: 1 addition & 1 deletion categories/tests/test_category_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def testMixingTabsSpaces(self):
Should raise an exception.
"""
string1 = ["cat1", " cat1-1", "\tcat1-2-FAIL!", ""]
string2 = ["cat1", "\tcat1-1", " cat1-2-FAIL!", ""]
string2 = ["cat2", "\tcat2-1", " cat2-2-FAIL!", ""]
cmd = Command()

# raise Exception
Expand Down
6 changes: 6 additions & 0 deletions categories/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.core.files import File
from django.core.files.uploadedfile import UploadedFile
from django.db.utils import IntegrityError

from categories.models import Category
from django.test import TestCase
Expand All @@ -19,3 +20,8 @@ def test_thumbnail(self):
self.assertEqual(category.pk, 1)
self.assertEqual(category.thumbnail_width, 640)
self.assertEqual(category.thumbnail_height, 480)

def test_duplicate_slug_fail(self):
Category.objects.create(name='Test Category', slug='test-category')
with self.assertRaises(IntegrityError):
Category.objects.create(name='Test Category1 ', slug='test-category')

0 comments on commit 83aeee0

Please sign in to comment.