Django

Code

Ticket #5996 (closed: fixed)

Opened 8 months ago

Last modified 8 months ago

psycopg2 raises "can't adapt" error where other backends work happily

Reported by: zgoda Assigned to: anonymous
Milestone: Component: Database wrapper
Version: SVN Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The description is vague, but I have only vague idea on what's going on. The problem does not manifests itself with sqlite3 and psycopg backends.

I have a model with CharField. Supplying (perfectly valid) unicode object that contains non-ASCII character to this field causes the psycopg2 to raise "Can't adapt" error on saving object. The traceback I get from ipython is like this:

/home/zgoda/www/zgodowie/blog/models.py in save(self)
     42         if not self.slug:
     43             self.slug = slughifi(self.title)
---> 44         super(Label, self).save()
     45
     46

/home/zgoda/www/.python/lib/python2.4/site-packages/django/db/models/base.py in save(self, raw)
    257                 cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
    258                     (qn(self._meta.db_table), ','.join(field_names),
--> 259                     ','.join(placeholders)), db_values)
    260             else:
    261                 # Create a new record with defaults for everything.

ProgrammingError: can't adapt 

It does not matter if I do a force_unicode() on slughifi() output, the error is the same.

The definition for the model is as follows:

class Label(models.Model):
    title = models.CharField(max_length=80, unique=True)
    slug = models.CharField(max_length=80, editable=False)
    description = models.TextField(null=True, blank=True)

    def save(self):
        if not self.slug:
            self.slug = slughifi(self.title)
        super(Label, self).save()

Attachments

pg_safeunicode.diff (1.3 kB) - added by remco@diji.biz on 12/01/07 08:02:45.
Unit test + fix

Change History

11/20/07 16:21:55 changed by zgoda

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Versions of libraries involved

In [2]: psycopg2.__version__
Out[2]: '2.0.6 (dec dt ext pq3)'

In [4]: psycopg.__version__
Out[4]: '1.1.21'

11/27/07 04:03:53 changed by sacrebis@wp.pl

I think I have the same problem, but it is related to SafeString? and SafeUnicode? objects.

In my case I have inclusion tag which accepts string as second argument.

{% some_inclusion_tag some_value %}

Inclusion tag code:

@inclusion_tag....
def some_inclusion_tag(value):
    obj = Klass.object.get(value=value)

and this raises "Can't adapt" error.

But look at that:

>>>  type(value)
<class 'django.utils.safestring.SafeUnicode'>

Could you check type of "self.title" ?

11/27/07 04:09:18 changed by zgoda

The title value is ordinary unicode object, but the slug value is SafeUnicode?. It looks like psycopg2 backend is unable to convert SafeUnicode? to ordinary unicode object (calling unicode() does the trick).

12/01/07 06:26:28 changed by remco@diji.biz

  • owner changed from nobody to anonymous.
  • status changed from new to assigned.

12/01/07 06:51:08 changed by remco@diji.biz

  • stage changed from Unreviewed to Accepted.

We have looked at the issue as part of the 01-dec sprint. The issue is psycopg2 specific. Psycopg2 is indeed not able to adapt the SafeUnicode? field even though SafeUnicode? is a subclass of unicode. We are thinking about adding a psycopg adapter for the SafeUnicode? type in the psycopg2 backend as proposed here: http://www.initd.org/tracker/psycopg/wiki/psycopg2_documentation#adaptation-of-python-values-to-sql-types

Unit tests + patch will be added soon

12/01/07 08:02:03 changed by remco@diji.biz

  • has_patch set to 1.
  • stage changed from Accepted to Ready for checkin.

Fixed by adding a field test for saving a SafeUnicode? object in a model and patching the psycopg2 backend.

See attached patch for both unit test and fix.

12/01/07 08:02:45 changed by remco@diji.biz

  • attachment pg_safeunicode.diff added.

Unit test + fix

12/01/07 16:26:24 changed by mtredinnick

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

(In [6816]) Fixed #5996 -- Add a pyscopg2 convertor for SafeUnicode? -> unicode. Thanks, remco@diji.biz


Add/Change #5996 (psycopg2 raises "can't adapt" error where other backends work happily)




Change Properties
Action