Opened 3 years ago

Closed 3 years ago

#33524 closed New feature (fixed)

ModelAdmin with defined radio_fields override empty_label

Reported by: Maxim Danilov Owned by: Hrushikesh Vaidya
Component: contrib.admin Version: 4.0
Severity: Normal Keywords: modeladmin, radio_fields, empty_label
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

ModelAdmin drops my "empty_label" and set "default_empty_label". For example:

class MyModelAdmin(ModelAdmin):
    radio_fields = 'myfield',

    def formfield_for_foreignkey(self, db_field, *args, **kwargs):
        if db_field.name == 'myfield':
            kwargs['empty_label'] = "I WANT TO SET MY OWN EMPTY LABEL"
        return super().formfield_for_foreignkey(db_field, *args, **kwargs)

You get never the "I WANT TO SET MY OWN EMPTY LABEL"

How to fix it:
In django\contrib\admin\options.py, row 234:

kwargs['empty_label'] = _('None') if db_field.blank else None

Should be changed on:

kwargs['empty_label'] = (kwargs.get('empty_label') or _('None')) if db_field.blank else None

Change History (8)

comment:1 by Mariusz Felisiak, 3 years ago

Has patch: unset
Triage Stage: UnreviewedAccepted
Type: BugNew feature

Agreed, empty_label from kwargs should take precedence over _('None').

comment:2 by revanthgss, 3 years ago

I want to work on this to get familiarised with contributing process. Assigning this to me.

Last edited 3 years ago by revanthgss (previous) (diff)

comment:3 by revanthgss, 3 years ago

Owner: changed from nobody to revanthgss
Status: newassigned

comment:4 by Hrushikesh Vaidya, 3 years ago

Has patch: set
Owner: changed from revanthgss to Hrushikesh Vaidya

Hey revanthgss, since we haven't heard from you in a while, I'm assigning this to myself :)
PR

in reply to:  4 comment:5 by revanthgss, 3 years ago

Replying to Hrushikesh Vaidya:

Hey revanthgss, since we haven't heard from you in a while, I'm assigning this to myself :)
PR

Sorry, I did not get time to check it out. All the best!!

comment:6 by Mariusz Felisiak, 3 years ago

Needs tests: set

comment:7 by Mariusz Felisiak, 3 years ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 119f227:

Fixed #33524 -- Allowed overriding empty_label for ForeignKey in ModelAdmin.radio_fields.

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