Adapting widget is not working
- Django better admin ArrayField version: 1.4.2
- Django version: 2.1.4
- Python version: 3.6.9
- Operating System: Linux
Description and What I did
I was trying to use the DynamicArrayTextareaWidget as described in the documentation:
from django_better_admin_arrayfield.forms.widgets import DynamicArrayTextareaWidget
class MyModelAdmin(OrderedModelAdmin, DynamicArrayMixin):
...
formfield_overrides = {
DynamicArrayField: {'widget': DynamicArrayTextareaWidget},
}
I also tried the workflow to create an own widget by following the given documentation:
class MyWidget(DynamicArrayWidget):
def __init__(self, *args, **kwargs):
kwargs['subwidget_form'] = MyForm
super().__init__(*args, **kwargs)
class MyModelAdmin(OrderedModelAdmin, DynamicArrayMixin):
...
formfield_overrides = {
DynamicArrayField: {'widget': MyWidget},
}
I use an OrderedModelAdmin instead of model.ModelAdmin.
Using the feature formfield_overrides works for me for models.Charfield etc. but not for the DynamicArrayField. I always get only the forms.TextInput rendered. When debugging, it appears that DynamicArrayTextareaWidget does not even get initialized.
Thanks for your help in advance!
@picodemark looks like there is a mistake in docs, try to use
formfield_overrides = { ArrayField: {'widget': MyWidget}, }
or
formfield_overrides = { ArrayField: {'widget': DynamicArrayTextareaWidget}, }
Hope it helps!
Hi @andreikaralkou,
I want to change the input field in the arrayfield to a textfield. I have tried to do what you say and added:
formfield_overrides = {
ArrayField: { 'widget': DynamicArrayTextareaWidget},
}
to the ModelAdmin. But sadly then I am unable to click on "Add another". Do you know what is going wrong?
This is part of my Models:
from django_better_admin_arrayfield.models.fields import ArrayField
class Provider(models.Model):
...
privacy_policies = ArrayField(models.TextField(), null=True, blank=True)
And the following from my admin:
from django_better_admin_arrayfield.admin.mixins import DynamicArrayMixin
from django_better_admin_arrayfield.forms.widgets import DynamicArrayTextareaWidget
from django_better_admin_arrayfield.models.fields import ArrayField
@admin.register(Provider)
class ProviderAdmin(admin.ModelAdmin, DynamicArrayMixin):
...
formfield_overrides = {
ArrayField: { 'widget': DynamicArrayTextareaWidget},
}
Thanks in advance
But sadly then I am unable to click on "Add another". Do you know what is going wrong?
@Nick-CookieFirst and if anyone else stumbles on this issue. It is caused by a JavaScript error in the admin page as the input field changes from an input element to a textarea and the JS script that runs that makes the "Add another" button work is unable to select the element. Specifically this part of the code:
https://github.com/gradam/django-better-admin-arrayfield/blob/b9349902ed4792759e32a1aa26b58cf42180bf8a/django_better_admin_arrayfield/static/js/django_better_admin_arrayfield.js#L29-L32
if you change .querySelector('input') to .querySelector("input,textarea") it solves the issue (Tested on Django 2.0 with Grappeli)
To update the JS, you can add a modified version of django_better_admin_arrayfield.min.js to your static folder. Make sure it's inside a js folder to match the original implementation. In my case I added it to
/<project>/<app>/templates/static/js/django_better_admin_arrayfield.min.js
(make sure to have the static directory set in settings.py under STATICFILES_DIRS)