You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/widgets/array.md
+18-2Lines changed: 18 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: ArrayWidget
3
3
order: 1
4
-
description: ArrayWidget for ArrayField
4
+
description: Learn how to use Django Unfold's ArrayWidget to efficiently manage and display ArrayField data in your Django admin interface with support for both text inputs and dropdown lists.
5
5
---
6
6
7
7
# Unfold widget ArrayWidget
@@ -15,22 +15,38 @@ INSTALLED_APPS = [
15
15
]
16
16
```
17
17
18
-
Below you can see how to use ArrayWidget in your admin class. In this example all `ArrayField` fields will use `ArrayWidget` to render input field. In case `choices` are provided for the widget, dropdown list will be used instead of text input.
18
+
Below you can see how to use ArrayWidget in your admin class. In this example, all `ArrayField` fields will use `ArrayWidget` to render input fields. If `choices` are provided for the widget, a dropdown list will be used instead of a text input.
19
+
20
+
When it comes to providing choices for the widget, by default the widget does not have any information about the field's choices, so it is mandatory to provide them manually. You can do this in the `get_form` method where the widget is initialized with the `choices` parameter.
19
21
20
22
```python
21
23
# admin.py
22
24
23
25
from django.contrib import admin
24
26
from django.contrib.postgres.fields import ArrayField
27
+
from django.db.models import TextChoices
28
+
from django.utils.translation import gettext_lazy as _
29
+
25
30
from unfold.admin import ModelAdmin
26
31
from unfold.contrib.forms.widgets import ArrayWidget
27
32
28
33
34
+
classSomeChoices(TextChoices):
35
+
OPTION_1="OPTION_1", _("Option 1")
36
+
OPTION_2="OPTION_2", _("Option 2")
37
+
38
+
29
39
@admin.register(MyModel)
30
40
classCustomAdminClass(ModelAdmin):
31
41
formfield_overrides = {
32
42
ArrayField: {
33
43
"widget": ArrayWidget,
34
44
}
35
45
}
46
+
47
+
# If you need to provide choices for the widget, you can do it in the get_form method.
0 commit comments