Code

Opened 2 years ago

Closed 4 months ago

#17556 closed New feature (duplicate)

Let Media classes to inherit

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

Description

Media classes in ModelAdmin or Form cannot inherit from other classes:

class BaseMedia:
    css = {"all": ["base.css", ] }

class ExampleAdmin(ModelAdmin):
    class Media(BaseMedia):
        js = ["example.js, ]    

In the example above, only js media will be available. This is because of the following code in django/forms/widgets.py:

class Media(StrAndUnicode):
    def __init__(self, media=None, **kwargs):
        if media:
            media_attrs = media.__dict__
        else:
            media_attrs = kwargs

__dict__ doesn't include inherited properties.

My proposal is to use getmembers from inspect module:

media_attrs = dict(getmembers(media))

I've attached a 2-lines patch. All unit tests passed against revision 17377.

Attachments (2)

media_inherit.diff (664 bytes) - added by ojo 2 years ago.
media_inherit.2.patch (1.7 KB) - added by d1ffuz0r 21 months ago.

Download all attachments as: .zip

Change History (6)

Changed 2 years ago by ojo

comment:1 Changed 2 years ago by lrekucki

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

The patch needs tests.

Changed 21 months ago by d1ffuz0r

comment:2 Changed 21 months ago by d1ffuz0r

  • Cc d1fffuz0r@… added
  • Easy pickings unset
  • Needs tests unset

tests for this patch

Version 0, edited 21 months ago by d1ffuz0r (next)

comment:3 Changed 4 months ago by aaugustin

This is a duplicate of #9357.

comment:4 Changed 4 months ago by charettes

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

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.