Code

Opened 8 years ago

Closed 7 years ago

#2866 closed enhancement (fixed)

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

Reported by: ymasuda <ymasuda@…> Owned by: adrian
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@…> 8 years ago.
Diff to add settings.DATABASE_OPTIONS and kwargs on DatabaseWrapper

Download all attachments as: .zip

Change History (5)

Changed 8 years ago by ymasuda <ymasuda@…>

Diff to add settings.DATABASE_OPTIONS and kwargs on DatabaseWrapper

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

  • Component changed from Admin interface to Database wrapper

comment:2 Changed 7 years ago by ludo

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 7 years ago by ymasuda <ymasuda@…>

  • Summary changed from Allowing DatabaseWrapper to support extra kwargs as settings.DATABASE_OPTIONS to [patch] Allowing DatabaseWrapper to support extra kwargs as settings.DATABASE_OPTIONS

comment:4 Changed 7 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

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

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.