Opened 9 years ago

Closed 9 years ago

#12224 closed (duplicate)

psycopg2 raises "can't adapt" when use gettext extensions on model meta permissions field.

Reported by: Felipe 'chronos' Prenholato Owned by: Felipe 'chronos' Prenholato
Component: Database layer (models, ORM) Version: master
Severity: Keywords: psycopg2, permissions, i18n, gettext, "can't adapt", syncdb
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no


When we create custom permissions for model, works perfectly if we use like this:

  class Meta:                                  
    permissions = (
      ('can_view_info',"Can view info."),
      ('can_view_reports',"Can view reports."),
      ('can_export_reports',"Can export reports."),
      ('can_search',"Can search."),

But, if we use like that, for multiple language sites:

  class Meta:                                  
    permissions = (
      ('can_view_info',_("Can view info.")),
      ('can_view_reports',_("Can view reports.")),
      ('can_export_reports',_("Can export reports.")),
      ('can_search',_("Can search.")),

We get psycopg2 raising following error on syncdb:

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): no                                             
Traceback (most recent call last):                                                         
  File "", line 11, in <module>                                                   
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/", line 439, in execute_manager                                                                                                                     
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/", line 380, in execute
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/", line 195, in run_from_argv                                                                                                                           
    self.execute(*args, **options.__dict__)                                                                                  
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/", line 222, in execute
    output = self.handle(*args, **options)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/", line 351, in handle
    return self.handle_noargs(**options)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/commands/", line 89, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/", line 194, in emit_post_sync_signal
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/dispatch/", line 166, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/contrib/auth/management/", line 28, in create_permissions
    defaults={'name': name, 'content_type': ctype})
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/", line 122, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/", line 328, in get_or_create
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/", line 419, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/", line 504, in save_base
    result = manager._insert(values, return_id=update_pk)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/", line 179, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/", line 1087, in insert_query
    return query.execute_sql(return_id)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/sql/", line 320, in execute_sql
    cursor = super(InsertQuery, self).execute_sql(None)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/sql/", line 2373, in execute_sql
    cursor.execute(sql, params)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/backends/", line 19, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: can't adapt

This error happens with Django 1.1 beta 1 and Django 1.2 pre-alpha (current svn). I tested with psycopg2 versions 2.0.12 and 2.0.13.

This problem probably is similar to SafeString and/or SafeUnicode issues on #5996.

Soon I'll try to fix this and post more info about problem.

Attachments (2)

ticket#1224.diff (1008 bytes) - added by Felipe 'chronos' Prenholato 9 years ago.
force_unicode fix for permissions
diff#1224_withtestcases.diff (3.6 KB) - added by cscortes 9 years ago.
Has patches and tests

Download all attachments as: .zip

Change History (7)

comment:1 Changed 9 years ago by Felipe 'chronos' Prenholato

Has patch: set

With help of SmileyChris on #django-dev I fixed this problem forcing unicode with force_unicode (my original idea was use mark_safe). Similar fix was did Alex in #10616.

Changed 9 years ago by Felipe 'chronos' Prenholato

Attachment: ticket#1224.diff added

force_unicode fix for permissions

comment:2 Changed 9 years ago by Jeremy Dunck

Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

Changed 9 years ago by cscortes

Has patches and tests

comment:3 Changed 9 years ago by cscortes

Patch needs improvement: unset

comment:4 Changed 9 years ago by Felipe 'chronos' Prenholato

I little doubt about this ticket ( I not have more time to work on ).
This 'fix' save string in unicode in database, that's ok, but we need a change on interfaces that show this string from DB (contrib.auth views for admin for now) to show translated strings (by ugettext) or a real fix using a new 'data type' in psycopg2 backend.

Some of two options can be considered? Which could be the guidelines?

comment:5 Changed 9 years ago by James Bennett

milestone: 1.2
Resolution: duplicate
Status: newclosed

This is a specific case of trying to have a field value stored in the database and marked for translation, which has been debated and, for the moment, wontfix'd. See #9601 for pointers to discussion.

Note: See TracTickets for help on using tickets.
Back to Top