I've learnt an interesting thing from developing with Django as a beginner a while ago - if the solution you think is obvious is not implemented then you're probably trying to work against a well developed and commonly agreed upon programming pattern, which is a good indication for me to perhaps do some more reading on the topic.
Best practices aside, I've run into a similar issue and while above philosophy and what Tonis said makes perfect sense, here's an alternative approach to merging initial and request data:
data = { **{ name: field.initial for name, field in YourFormClass.declared_fields.items( ) }, **getattr(request, request.method).dict(), }
The way it works is it merges initial values defined inside YourFormClass with data from the request. Downsides? Won't work with multiple valued keys. Probably others, too.
Comment
I've learnt an interesting thing from developing with Django as a beginner a while ago - if the solution you think is obvious is not implemented then you're probably trying to work against a well developed and commonly agreed upon programming pattern, which is a good indication for me to perhaps do some more reading on the topic.
Best practices aside, I've run into a similar issue and while above philosophy and what Tonis said makes perfect sense, here's an alternative approach to merging initial and request data:
data = {
**{
name: field.initial
for name, field in YourFormClass.declared_fields.items(
)
},
**getattr(request, request.method).dict(),
}
The way it works is it merges initial values defined inside YourFormClass with data from the request. Downsides? Won't work with multiple valued keys. Probably others, too.