Code

Opened 3 years ago

Closed 3 years ago

#14695 closed Bug (fixed)

Field's `name` kwarg gets paved over

Reported by: erikrose Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords: dceu2011
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Field.set_attributes_from_name should [source:/django/trunk/django/db/models/fields/__init__.py?rev=14069#L229 set self.name = name] only if self.name is None. As things stand, any name=whatever kwarg we pass to a field on construction is obliterated by set_attributes_from_name. This means things like…

_category = models.IntegerField(db_column='category', name='category')

…end up with a get__category_display method (note the 2 underscores in a row) rather than a get_category_display. I've attached a quick but functional patch that fixes this; the tests (in 1.2.3, anyway) still all pass. One possible improvement might be to use get_attname rather than saying self.name directly, but (1) I'm not sure what the layering intentions are in there and (2) it blew up in a hairly-looking way when I tried—so I wouldn't mind some clueful feedback about that.

FWIW, my use case is replacing my model's category attr with a descriptor so I can do some custom stuff on get. #3148 would make this unnecessary for me, but I could see that taking awhile to land.

Attachments (2)

field_name.diff (782 bytes) - added by erikrose 3 years ago.
Patch against Django 1.2.3
ticket14695.patch (2.6 KB) - added by willhardy 3 years ago.
Adding tests

Download all attachments as: .zip

Change History (10)

Changed 3 years ago by erikrose

Patch against Django 1.2.3

comment:1 Changed 3 years ago by erikrose

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Here's a proper link to the line that does the paving over: source:/django/trunk/django/db/models/fields/__init__.py@14069#L229.

comment:2 Changed 3 years ago by russellm

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

comment:3 Changed 3 years ago by erikrose

  • Owner changed from nobody to erikrose

comment:4 Changed 3 years ago by jaddison

  • Severity set to Normal
  • Type set to Bug

comment:5 Changed 3 years ago by willhardy

  • Easy pickings unset
  • Owner changed from erikrose to willhardy
  • Status changed from new to assigned
  • UI/UX unset

Changed 3 years ago by willhardy

Adding tests

comment:6 Changed 3 years ago by willhardy

  • Keywords dceu2011 added
  • Needs tests unset
  • Owner changed from willhardy to nobody
  • Patch needs improvement unset
  • Status changed from assigned to new

comment:7 Changed 3 years ago by ojii

  • Triage Stage changed from Accepted to Ready for checkin

comment:8 Changed 3 years ago by jezdez

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

In [16614]:

Fixed #14695 -- Stopped model fields from incorrectly overwriting the field name during model initialization if it was already passed as a keyword argument. Thanks, erikrose and willhardy.

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.