Proposal: unique by default on SlugFields

91 views
Skip to first unread message

Meshy

unread,
Jun 27, 2012, 7:15:03 AM6/27/12
to django-d...@googlegroups.com
Perhaps it's just me, but I've very rarely wanted a SlugField that wasn't unique. Would this not be a sensible default? I realise that a lot of apps will rely upon this default, but objectively speaking would this not be better? Perhaps this change would be appropriate for django 2.0.

At the moment, slug = models.SlugField()creates a non-unique field, and if you want it to be unique, then you must add unique=True. I feel this is wrong.

It seems to me that unique should be default, and if you don't want a unique slug, you should explicitly state that:
slug = models.SlugField(unique=False)

I've added an issue on the tracker for this: https://code.djangoproject.com/ticket/18525

I realise this may be a contentious issue...or that I may even get shot down in flames on this one ;P Go easy on me! :)

What do you all think?

Charlie.

Dan Watson

unread,
Jun 27, 2012, 8:25:04 AM6/27/12
to django-d...@googlegroups.com
On Wednesday, June 27, 2012 7:15:03 AM UTC-4, Meshy wrote:
Perhaps it's just me, but I've very rarely wanted a SlugField that wasn't unique. Would this not be a sensible default? I realise that a lot of apps will rely upon this default, but objectively speaking would this not be better? Perhaps this change would be appropriate for django 2.0.

At the moment, slug = models.SlugField()creates a non-unique field, and if you want it to be unique, then you must add unique=True. I feel this is wrong.

It seems to me that unique should be default, and if you don't want a unique slug, you should explicitly state that:
slug = models.SlugField(unique=False)

I'd agree that slugs are often unique, but I think it's just as common that they are unique together with another field (think blogs -- /blog/2012/06/27/some-slug/ -- where the slug is unique_together with the date). For this case, you'd have to remember to specify unique=False *and* unique_together.
 
I've added an issue on the tracker for this: https://code.djangoproject.com/ticket/18525

I realise this may be a contentious issue...or that I may even get shot down in flames on this one ;P Go easy on me! :)

What do you all think?

It doesn't seem like a burden to have to say unique=True if you truly want a singularly unique slug. Making it explicit, aside from being good python zen, also helps to see the natural keys of your model at a glance.

Dan

Russell Keith-Magee

unread,
Jun 27, 2012, 7:34:49 PM6/27/12
to django-d...@googlegroups.com
I don't think it's particularly contentious -- I'd certainly pay your
premise that Slugs are almost always unique (although as Dan pointed
out, they're often unique with other fields). However, I would make
two comments:

* There's a backwards compatibility issue. We can't just reverse the
value of the setting, because it would break all existing code. There
might be some ways to introduce it slowly (e.g., the value of an
"undefined" unique setting changes over a couple of releases).
However, even if we were going to go down this path, we still need to
balance the inconvenience of the change against the benefits.

* Explicit is better than implicit. By default, database fields won't
be unique -- you need to explicitly make them unique. Even though
there's a little bit more typing involved, there's something to be
said for being explicit about making every field unique.

If we had a clean slate, it might be worth reconsidering the default.
However, at this point, the behaviour of SlugField has been the way it
has for 6 years, and it has been, at worst, a minor inconvenience. For
me, given the inconvenience involved in making the change, this issue
just doesn't rise above "meh" level.

Yours,
Russ Magee %-)
Reply all
Reply to author
Forward
0 new messages