Code

Opened 4 years ago

Closed 4 years ago

#12224 closed (duplicate)

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

Reported by: chronos Owned by: chronos
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: UI/UX:

Description

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 "manage.py", line 11, in <module>                                                   
    execute_manager(settings)                                                              
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/__init__.py", line 439, in execute_manager                                                                                                                     
    utility.execute()                                                                                                        
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/__init__.py", line 380, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)                                                                  
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/base.py", line 195, in run_from_argv                                                                                                                           
    self.execute(*args, **options.__dict__)                                                                                  
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/base.py", line 222, in execute
    output = self.handle(*args, **options)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/base.py", line 351, in handle
    return self.handle_noargs(**options)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/core/management/commands/syncdb.py", 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/sql.py", line 194, in emit_post_sync_signal
    interactive=interactive)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/dispatch/dispatcher.py", 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/__init__.py", line 28, in create_permissions
    defaults={'name': name, 'content_type': ctype})
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/manager.py", 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/query.py", line 328, in get_or_create
    obj.save(force_insert=True)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/base.py", 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/base.py", 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/manager.py", line 179, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/models/query.py", 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/subqueries.py", 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/query.py", line 2373, in execute_sql
    cursor.execute(sql, params)
  File "/home/felipe.prenholato/.python/lib/python2.5/site-packages/django/db/backends/util.py", 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 chronos 4 years ago.
force_unicode fix for permissions
diff#1224_withtestcases.diff (3.6 KB) - added by cscortes 4 years ago.
Has patches and tests

Download all attachments as: .zip

Change History (7)

comment:1 Changed 4 years ago by chronos

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 4 years ago by chronos

force_unicode fix for permissions

comment:2 Changed 4 years ago by jdunck

  • Needs tests set
  • Patch needs improvement set
  • Triage Stage changed from Unreviewed to Accepted

Changed 4 years ago by cscortes

Has patches and tests

comment:3 Changed 4 years ago by cscortes

  • Patch needs improvement unset

comment:4 Changed 4 years ago by chronos

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 4 years ago by ubernostrum

  • milestone 1.2 deleted
  • Resolution set to duplicate
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.