unique_for_date not working?

295 views
Skip to first unread message

Ryan Osborn

unread,
Mar 25, 2011, 4:55:31 AM3/25/11
to Django users
Hi,

I am using django 1.3 and have the following in my models.py:

class Entry(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(unique_for_date='created')
created = models.DateField(auto_now_add=True, editable=False)

However, I can add an entry in the admin interface with a slug and
then immediately afterwards add another entry with the same slug
without an error being rasied. Am I missing something?

Thanks,

Ryan

Jacob Kaplan-Moss

unread,
Mar 25, 2011, 9:54:26 AM3/25/11
to django...@googlegroups.com
Hi Ryan --

Basically what's happening is that `auto_now_add` and `editable`
outdated kludges. Both work by actually excluding the field in
question from the admin form, which means it's not available for
validation so none is ran.

A better approach, I think, would be to use something like::

import datetime
from django.db import models

class Entry(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(unique_for_date='created')

created = models.DateField(default=datetime.date.today)

This will work as expected in the admin and in model forms.

Jacob

Ryan Osborn

unread,
Mar 25, 2011, 11:01:47 AM3/25/11
to Django users
Thanks for your reply Jacob. The only problem with the method you
suggested is that the field now shows up in the admin form which
allows people to edit it. I would rather that this didn't happen. Is
there any way around this?

Thanks,

Ryan

On Mar 25, 1:54 pm, Jacob Kaplan-Moss <ja...@jacobian.org> wrote:
> Hi Ryan --
>

Daniel Roseman

unread,
Mar 25, 2011, 11:15:15 AM3/25/11
to django...@googlegroups.com, Ryan Osborn
On Friday, March 25, 2011 3:01:47 PM UTC, Ryan Osborn wrote:
Thanks for your reply Jacob.  The only problem with the method you
suggested is that the field now shows up in the admin form which
allows people to edit it.  I would rather that this didn't happen.  Is
there any way around this?

Thanks,

Ryan

Add it to the `exclude` list in your admin class.
--
DR.

Ryan Osborn

unread,
Mar 25, 2011, 11:48:07 AM3/25/11
to Django users
The problem with this is that the unique_for_date no longer works
again

Andre Terra

unread,
Mar 25, 2011, 12:17:55 PM3/25/11
to django...@googlegroups.com, Ryan Osborn
Overwrite the model's save method and add the date automatically on save. That is the best way to do most things "auto" in your models.


Sincerely,
Andre Terra

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Karen Tracey

unread,
Mar 25, 2011, 4:42:20 PM3/25/11
to django...@googlegroups.com
On Fri, Mar 25, 2011 at 12:17 PM, Andre Terra <andre...@gmail.com> wrote:
Overwrite the model's save method and add the date automatically on save. That is the best way to do most things "auto" in your models.


But if you do this, you are past the point where you can flag non-unique for the date as an error on form submission. The fact that admin doesn't include readonly (excluded from form) fields in the unique checks is ticket #13091:

http://code.djangoproject.com/ticket/13091

Karen 
--
http://tracey.org/kmt/

Derek

unread,
Mar 28, 2011, 3:03:21 AM3/28/11
to Django users
On Mar 25, 10:42 pm, Karen Tracey <kmtra...@gmail.com> wrote:
As "bystander" here... what then is the actual (best and definitive)
solution to the OP's problem?
Reply all
Reply to author
Forward
0 new messages