Code

Opened 2 years ago

Closed 14 months ago

Last modified 8 months ago

#18162 closed Bug (fixed)

Specifying choices to Field overrides any form_class argument

Reported by: rafallo Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords:
Cc: rafallo Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

In django.db.models.fields.Field (and derivatives) function formfield allow changing form_class when method is invoked manually. But changing form_class when 'choices' specified is impossible.

I attach patch which add 'form_class_for_choices' in function kwargs.

Attachments (1)

form_class_for_choices.patch (726 bytes) - added by rafallo 2 years ago.

Download all attachments as: .zip

Change History (8)

Changed 2 years ago by rafallo

comment:1 Changed 2 years ago by rafallo

  • Cc rafallo added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 23 months ago by andrewgodwin

  • Component changed from Database layer (models, ORM) to Forms
  • Patch needs improvement set
  • Summary changed from formfield in Field class should allow change form_class if choices specified to Specifying choices to Field overrides any form_class argument
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from New feature to Bug

Valid bug, but attached patch is not what I would do - instead, just make it respect form_class if provided (by changing the default value so assignment of it is detectable.)

comment:3 Changed 14 months ago by oinopion

I've made an effort to fix this in proposed way. The problem is, Django depends on ignoring given form_class, for example IntegerField.formfield passes form_class regardless of choices. Here is my branch, where I added override, where subclasses of TypedChoiceField are not ignored. This preserves current behaviour while adding some flexibility: https://github.com/oinopion/django/compare/ticket18162

comment:4 Changed 14 months ago by Honza Král <Honza.Kral@…>

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

In 10026c2ad0da99591567ba13afe4b489e050ca4a:

Merge pull request #804 from oinopion/ticket18162

Fixes #18162 -- Specifying choices to Field overrides any form_class argument

comment:5 Changed 8 months ago by carljm

Just FYI for anyone involved with this ticket, I've opened #20999 to make the case that the wrong fix was applied here, and rather than unnecessarily-restrictive type checking we should instead use rafallo's original patch respecting a new keyword argument.

comment:6 Changed 8 months ago by Carl Meyer <carl@…>

In 7211741fc5d50425133ab942181cc095c56d7387:

Fixed #20999 - Allow overriding formfield class with choices, without subclass restrictions.

Refs #18162. Thanks claudep and mjtamlyn for review.

comment:7 Changed 8 months ago by Carl Meyer <carl@…>

In 21a3efcf48559b3336ca1743d94c741a82feffd6:

[1.6.x] Fixed #20999 - Allow overriding formfield class with choices, without subclass restrictions.

Refs #18162. Thanks claudep and mjtamlyn for review.

Backport of 7211741fc5d50425 from master.

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.