Opened 4 years ago
Closed 4 years ago
#32467 closed Cleanup/optimization (invalid)
django admin widget.attrs not work with ForeignKey
Reported by: | Hassan Igbaria | Owned by: | Hassan Igbaria |
---|---|---|---|
Component: | Forms | Version: | 3.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
self.fields['parent'].widget.attrs = {'class': 'custom-class'}
parent is ForeignKey field
class Category(models.Model): title = models.CharField(_('Title'), max_length=300) description = models.TextField(_('Description'), null=True, blank=True) is_active = models.BooleanField(_('Active ?'), default=True) image = models.ImageField( _('Image'), upload_to="category/", null=True, blank=True ) parent = models.ForeignKey( "self", verbose_name=_('Parent'), on_delete=models.SET_NULL, null=True, blank=True )
Change History (7)
comment:1 by , 4 years ago
Description: | modified (diff) |
---|
comment:2 by , 4 years ago
Description: | modified (diff) |
---|---|
Owner: | changed from | to
Status: | new → assigned |
comment:3 by , 4 years ago
Description: | modified (diff) |
---|
comment:4 by , 4 years ago
Resolution: | → invalid |
---|---|
Status: | assigned → closed |
comment:5 by , 4 years ago
self.fields['parent'].widget.attrs.update({'class': 'custom-class'})
It's not work with parent
field but it's work with description
field
comment:6 by , 4 years ago
Resolution: | invalid |
---|---|
Status: | closed → new |
comment:7 by , 4 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Please don't reopen closed ticket and don't use Trac as a support channel.
Closing per TicketClosingReasons/UseSupportChannels.
Note:
See TracTickets
for help on using tickets.
In your example, I think you should be using the
update
method rather than directly assigning a dict. There's a great example of this in thedocs.
So I think your example should be:
self.fields['parent'].widget.attrs.update({'class': 'custom-class'})