django-crispy-forms / django-crispy-forms/django-crispy-forms

Set and delete Helper fields by string key

Open
#1,362 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
5.2k
Forks
731
PR merge metrics
No merged PRs in 30d

Description

* Package version: 2.0
* Django version: 3.2.18
* Python version: 3.8.17
* Template pack: crispy-bootstrap5 0.7

### Description:

I am conditionally changing the form by adding and removing its fields. I follow [the guide to dynamic layouts](https://django-crispy-forms.readthedocs.io/en/latest/dynamic_layouts.html), but I don't like the indexed addressing because of its WETness: If I add a new field, I'll have to update the indices. [There is a way to select a field using its name](https://django-crispy-forms.readthedocs.io/en/latest/dynamic_layouts.html#selecting-a-field-name), but it doesn't work if I want to delete a field.

```python
name_field = self.helper["name"] # fine
del self.helper["name"] # TypeError: list indices must be integers or slices, not str
```

#### Proposed solution

Extract the key finding logic from `DynamicLayoutHandler.__get_item__` and make it available for `__del_item__` (and `__set_item__`, for that matter)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.