[Django] #32467: django admin widget.attrs not work with ForeignKey

23 views
Skip to first unread message

Django

unread,
Feb 20, 2021, 9:08:31 AM2/20/21
to django-...@googlegroups.com
#32467: django admin widget.attrs not work with ForeignKey
------------------------------------------------+------------------------
Reporter: Hassan Igbaria | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Forms | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
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
)

--
Ticket URL: <https://code.djangoproject.com/ticket/32467>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 20, 2021, 9:09:50 AM2/20/21
to django-...@googlegroups.com
#32467: django admin widget.attrs not work with ForeignKey
-------------------------------------+-------------------------------------

Reporter: Hassan Igbaria | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Hassan Igbaria:

Old description:

> 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
> )

New description:

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
)

}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32467#comment:1>

Django

unread,
Feb 20, 2021, 9:11:59 AM2/20/21
to django-...@googlegroups.com
#32467: django admin widget.attrs not work with ForeignKey
-------------------------------------+-------------------------------------
Reporter: Hassan Igbaria | Owner: Hassan
Type: | Igbaria
Cleanup/optimization | Status: assigned
Component: Forms | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hassan Igbaria):

* owner: nobody => Hassan Igbaria
* status: new => assigned


Old description:

> 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
> )
>
> }}}

New description:

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
)

}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32467#comment:2>

Django

unread,
Feb 20, 2021, 9:13:06 AM2/20/21
to django-...@googlegroups.com
#32467: django admin widget.attrs not work with ForeignKey
-------------------------------------+-------------------------------------
Reporter: Hassan Igbaria | Owner: Hassan
Type: | Igbaria
Cleanup/optimization | Status: assigned
Component: Forms | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Hassan Igbaria:

Old description:

> 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
> )
>
> }}}

New description:

{{{ 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
)

}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32467#comment:3>

Django

unread,
Feb 21, 2021, 4:38:24 AM2/21/21
to django-...@googlegroups.com
#32467: django admin widget.attrs not work with ForeignKey
-------------------------------------+-------------------------------------
Reporter: Hassan Igbaria | Owner: Hassan
Type: | Igbaria
Cleanup/optimization | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by David Smith):

* status: assigned => closed
* resolution: => invalid


Comment:

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 the
[https://docs.djangoproject.com/en/3.1/ref/forms/widgets/#styling-widget-
instances docs].

So I think your example should be:
`self.fields['parent'].widget.attrs.update({'class': 'custom-class'})`

--
Ticket URL: <https://code.djangoproject.com/ticket/32467#comment:4>

Django

unread,
Feb 23, 2021, 7:30:40 AM2/23/21
to django-...@googlegroups.com
#32467: django admin widget.attrs not work with ForeignKey
-------------------------------------+-------------------------------------
Reporter: Hassan Igbaria | Owner: Hassan
Type: | Igbaria
Cleanup/optimization | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Hassan Igbaria):

{{{ self.fields['parent'].widget.attrs.update({'class': 'custom-class'})
}}}

It's not work with `parent` field but it's work with `description` field

--
Ticket URL: <https://code.djangoproject.com/ticket/32467#comment:5>

Django

unread,
Feb 26, 2021, 6:46:20 AM2/26/21
to django-...@googlegroups.com
#32467: django admin widget.attrs not work with ForeignKey
-------------------------------------+-------------------------------------
Reporter: Hassan Igbaria | Owner: Hassan
Type: | Igbaria
Cleanup/optimization | Status: new
Component: Forms | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hassan Igbaria):

* status: closed => new
* resolution: invalid =>


--
Ticket URL: <https://code.djangoproject.com/ticket/32467#comment:6>

Django

unread,
Feb 26, 2021, 2:18:28 PM2/26/21
to django-...@googlegroups.com
#32467: django admin widget.attrs not work with ForeignKey
-------------------------------------+-------------------------------------
Reporter: Hassan Igbaria | Owner: Hassan
Type: | Igbaria
Cleanup/optimization | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

Please don't reopen closed ticket and don't use Trac as a support channel.

Closing per TicketClosingReasons/UseSupportChannels.

--
Ticket URL: <https://code.djangoproject.com/ticket/32467#comment:7>

Reply all
Reply to author
Forward
0 new messages