⬅︎ Back to How to have default/initial values in a Django form that is bound and rendered
The solution comes down to merging the `data` and `initial` dicts, with a preference for the content of `data`. Using an idiomatic merge would outline this behaviour. https://treyhunner.com/2016/02/how-to-merge-dictionaries-in-python/ has a bunch of those.```pythondef __init__(self, data, **kwargs): initial = kwargs.get('initial', {}) data = {**initial, **data} super().__init__(data, **kwargs)```
That's neat! It's ultimately sugar syntax to my otherwise clunky loop where I merge `data` and `kwargs.get('initial')`. Thanks!
Comment
The solution comes down to merging the `data` and `initial` dicts, with a preference for the content of `data`. Using an idiomatic merge would outline this behaviour. https://treyhunner.com/2016/02/how-to-merge-dictionaries-in-python/ has a bunch of those.
```python
def __init__(self, data, **kwargs):
initial = kwargs.get('initial', {})
data = {**initial, **data}
super().__init__(data, **kwargs)
```
Replies
That's neat! It's ultimately sugar syntax to my otherwise clunky loop where I merge `data` and `kwargs.get('initial')`. Thanks!