stevencoding / django-basic-apps

Automatically exported from code.google.com/p/django-basic-apps
0 stars 0 forks source link

blog.models.Post.slug is not a unique field, duplicate slugs cause errors for many date-based views #9

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Perhaps Post's slug field should be unique?  If several Posts have
duplicate slugs, many date-based detail views raise unhandled exceptions
from their .get() methods.

Maybe there's a compelling reason why duplicate slugs should be allowed?  I
can't imagine a use case that would really require them.

Example exception below:

Environment:

Request Method: GET
Request URL: http://mysite.net/weblog/2005/may/5/poast/
Django Version: 1.0-alpha-SVN-8194
Python Version: 2.4.4
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'django.contrib.flatpages',
 'tagging',
 'basic.inlines',
 'basic.blog']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware')

Traceback:
File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py" in
get_response
  87.                 response = callback(request, *callback_args,
**callback_kwargs)
File "/var/www/mysite/django_project/basic/blog/views.py" in post_detail
  63.         queryset = Post.objects.published(),
File "/usr/lib/python2.4/site-packages/django/views/generic/date_based.py"
in object_detail
  326.         obj = queryset.get(**lookup_kwargs)
File "/usr/lib/python2.4/site-packages/django/db/models/query.py" in get
  303.         raise self.model.MultipleObjectsReturned("get() returned
more than one %s -- it returned %s! Lookup parameters were %s"

Exception Type: MultipleObjectsReturned at /weblog/2005/may/5/poast/
Exception Value: get() returned more than one Post -- it returned 2! Lookup
parameters were {'slug__exact': u'poast', 'publish__range':
(datetime.datetime(2005, 5, 5, 0, 0), datetime.datetime(2005, 5, 5, 23, 59,
59, 999999))}

Original issue reported on code.google.com by AxisOfEn...@gmail.com on 30 Aug 2008 at 6:44

GoogleCodeExporter commented 8 years ago
Thank you, this is now fixed in trunk.

Original comment by npbor...@gmail.com on 2 Sep 2008 at 4:41

GoogleCodeExporter commented 8 years ago
Is this really fixed?  I think the problem is that the publish field is a 
datetime
object so it's almost always unique.  If you want to make sure the URL is 
unique  you
need to use a DateField instead of DateTime Field.  Otherwise you haven't 
solved the
MultipleObjectsReturned problem.

In [23]: from basic.blog.models import Post

In [24]: from django.contrib.auth.models import User

In [25]: u=User.objects.all()[0]

In [26]: from datetime import datetime

In [27]: q=Post(title='foobar',slug='fbar',author=u,publish=datetime.now())

In [28]: p=Post(title='foobar',slug='fbar',author=u,publish=datetime.now())

In [29]: q.save()

In [30]: p.save()

In [31]: Post.objects.get(slug='fbar')
---------------------------------------------------------------------------
MultipleObjectsReturned                   Traceback (most recent call last)

/Users/mandric/dev/jschool/code/projects/kdmc/<ipython console> in <module>()

/opt/local/lib/python2.5/site-packages/django/db/models/manager.py in get(self,
*args, **kwargs)
     91 
     92     def get(self, *args, **kwargs):
---> 93         return self.get_query_set().get(*args, **kwargs)
     94 
     95     def get_or_create(self, **kwargs):

/opt/local/lib/python2.5/site-packages/django/db/models/query.py in get(self, 
*args,
**kwargs)
    309                     % self.model._meta.object_name)
    310         raise self.model.MultipleObjectsReturned("get() returned more than
one %s -- it returned %s! Lookup parameters were %s"
--> 311                 % (self.model._meta.object_name, num, kwargs))
    312 
    313     def create(self, **kwargs):

MultipleObjectsReturned: get() returned more than one Post -- it returned 2! 
Lookup
parameters were {'slug': 'fbar'}

Original comment by mand...@gmail.com on 16 Feb 2009 at 6:09