Django

Code

Ticket #2192 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

[patch] DateField with unique=True breaks

Reported by: anonymous Assigned to: adrian
Milestone: Component: django.contrib.admin
Version: SVN Keywords: DateField unique breaks
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The following model breaks, when I try to add an object in the admin.

from django.db import models

class Foo(models.Model):
    the_date = models.DateField('Watch me screw up', unique=True)

    class Admin:
        pass

With the following error:

AttributeError at /admin/foo/foo/add/
'str' object has no attribute 'strftime'
Request Method: 	POST
Request URL: 	http://localhost/admin/foo/foo/add/
Exception Type: 	AttributeError
Exception Value: 	'str' object has no attribute 'strftime'
Exception Location: 	/home/anonymous/django/latest/django/db/models/fields/__init__.py in get_db_prep_lookup, line 415

Attachments

datefield_unique.diff (0.6 kB) - added by deryck@samba.org on 06/24/06 21:10:07.
Convert string to datetime.date in DateField?.get_db_prep_lookup if needed.
datefield_unique.2.diff (0.6 kB) - added by deryck@samba.org on 06/26/06 07:27:26.
New version of patch. Just check for string first, rather than convert to object, only to go back to string.

Change History

06/18/06 23:32:01 changed by ubernostrum

After some poking around, it looks like the problem here is that when django.db.models.fields.manipulator_validator_unique tries to do a lookup to see if an object with the same date exists, it's not turning the string into a datetime.date object, just continuing to handle it as a string. I'd imagine that trying this with a DateTimeField or TimeField would yield similar problems.

So it looks like we need to do some introspection of the field type in there and convert to an appropriate type for doing the lookup.

06/20/06 12:03:53 changed by anonymous

  • priority changed from normal to high.

06/22/06 16:54:29 changed by anonymous

  • severity changed from normal to major.

06/24/06 21:00:12 changed by deryck@samba.org

This only happens with DateField. DateTimeField and TimeField work fine. The cause is the following lines added in r2517 to db.models.fields.DateField.get_db_prep_lookup:

elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'ne'): 
    value = value.strftime('%Y-%m-%d')

I don't follow the commit log, but it seems odd this would be needed just for DateFields and not the rest. The following is a workaround if the lines need to stay. Just convert back to datetime.date if needed.

Patch:

--- django/db/models/fields/__init__.py (revision 3194)
+++ django/db/models/fields/__init__.py (working copy)
@@ -412,6 +412,8 @@
         if lookup_type == 'range':
             value = [str(v) for v in value]
         elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'ne'):
+            if type(value) is str:
+                value = self.to_python(value)
             value = value.strftime('%Y-%m-%d')
         else:
             value = str(value)

06/24/06 21:10:07 changed by deryck@samba.org

  • attachment datefield_unique.diff added.

Convert string to datetime.date in DateField?.get_db_prep_lookup if needed.

06/24/06 21:12:32 changed by deryck@samba.org

  • summary changed from DateField with unique=True breaks to [patch] DateField with unique=True breaks.

Change Summary to indicate patch. (Sorry about the first inline patch.)

06/26/06 07:27:26 changed by deryck@samba.org

  • attachment datefield_unique.2.diff added.

New version of patch. Just check for string first, rather than convert to object, only to go back to string.

06/27/06 21:43:11 changed by anonymous

this patch seemed to do the trick for me

06/28/06 06:37:18 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [3223]) Fixed #1754, #2211, #2192 -- allow date filtering comparisons to use strings as well as date objects. Fixed a couple of admin crashes as well.


Add/Change #2192 ([patch] DateField with unique=True breaks)




Change Properties
Action