2

Django Guardian has two forms defined in admin.py, GroupManage and UserManage: https://github.com/lukaszb/django-guardian/blob/master/guardian/admin.py#L368

I would like to add auto-completion to these two forms, and the best way I assume to make that happen is to overwrite the group and user's field widgets (my first attempt uses django autocomplete_light.) The goal is to not need to fork django guardian.

So in my app's models.py, I added the following code

GroupManage.__class__.group = forms.CharField(max_length=81,
    error_messages={'does_not_exist':
    "This group does not exist!"}, widget=ChoiceWidget(True))

I also tried using setattr to no avail. In the django shell it acts like this should be working, but when the admin page gets loaded the old group variable is restored, with the default CharField widget.

zgohr
  • 678
  • 7
  • 8
  • just override the admin template for the model. – Paulo Scardine Aug 07 '12 at 17:51
  • This would defeat the purpose of using an existing django app to handle the autocompletion. A valid solution, but not what I was looking to do. – zgohr Aug 07 '12 at 17:57
  • Note that autocomplete-light [does not support CharField](http://www.djangopackages.com/grids/g/auto-complete/) yet, however, CharField support is under [active development](https://github.com/yourlabs/django-autocomplete-light/issues/32) – jpic Aug 21 '12 at 12:44
  • Just in case anyone needs a link to the originally-referenced code: https://github.com/django-guardian/django-guardian/blob/v1.0.4/guardian/admin.py#L368 – Wesley B Jun 07 '22 at 21:39

1 Answers1

1

The fields defined for the class are stored in the dictionary base_fields.

GroupManage.base_fields['group'] = forms.CharField(max_length=81,
error_messages={'does_not_exist':
"This group does not exist!"}, widget=ChoiceWidget(True))

Sometimes, it might be easier to alter a field attribute instead of replacing the entire field:

GroupManage.base_fields['group'].help_text = "New help text"
Alasdair
  • 298,606
  • 55
  • 578
  • 516