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.
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>
* 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>
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>
* 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>
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>
* status: closed => new
* resolution: invalid =>
--
Ticket URL: <https://code.djangoproject.com/ticket/32467#comment:6>
* 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>