Opened 10 years ago

Closed 10 years ago

#2866 closed enhancement (fixed)

[patch] Allowing DatabaseWrapper to support extra kwargs as settings.DATABASE_OPTIONS

Reported by: ymasuda <ymasuda@…> Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

It would be nice to support extra keyword arguments on backends.*.DatabaseWrapper to tweak backend-specific behaviour of connect().

For example, MySQLdb supports charset keyword argument that can be used to set-default-character-set as utf8.
Allowing extra keyword arguments works as follows:

  1. specify {'charset': 'utf8'} on settings.DATABASE_OPTIONS (which defaults to empty dictionary)
  2. settings.DATABASE_OPTIONS is passed to DatabaseWrapper as keyword arguments: (connection = backend.DatabaseWrapper(**settings.DATABASE_OPTIONS))
  3. DatabaseWrapper.init() saves kwargs as self.options
  4. on DatabaseWrapper.cursor(), which build connection as needed, passes self.options as extra keyword argument: self.connection = Database.connect(**kwargs)

Attached diff includes changes on django.conf.global_settings, django.db.init, and django.db.backends.*.base.
Please note that currently kwargs are just ignored on ado_mssql (that backend does not support extra kwargs).

Attachments (1)

ticket2866.diff (8.4 KB) - added by ymasuda <ymasuda@…> 10 years ago.
Diff to add settings.DATABASE_OPTIONS and kwargs on DatabaseWrapper

Download all attachments as: .zip

Change History (5)

Changed 10 years ago by ymasuda <ymasuda@…>

Attachment: ticket2866.diff added

Diff to add settings.DATABASE_OPTIONS and kwargs on DatabaseWrapper

comment:1 Changed 10 years ago by ymasuda <ymasuda@…>

Component: Admin interfaceDatabase wrapper

comment:2 Changed 10 years ago by Ludovico Magnocavallo

Being able to pass an additional 'charset' argument to the MySQLdb connection is very important, as there are many occasions when the backend will raise the dreaded "Warning: Data truncated for column '...' at row ..." otherwise. Please commit this patch, or one of the others found in similar tickets.

comment:3 Changed 10 years ago by ymasuda <ymasuda@…>

Summary: Allowing DatabaseWrapper to support extra kwargs as settings.DATABASE_OPTIONS[patch] Allowing DatabaseWrapper to support extra kwargs as settings.DATABASE_OPTIONS

comment:4 Changed 10 years ago by Jacob

Resolution: fixed
Status: newclosed

(In [4048]) Fixed #2866: Added DATABASE_OPTIONS setting which gets passed as extra kwargs to the backend's connect method. Thanks, ymasuda.

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