设置初始表单数据

Setting inital form data

下面我粘贴了用户个人资料编辑视图的片段,我认为没有必要在这里粘贴所有视图。我想设置初始数据,当用户想要编辑他的个人资料时,他应该看到实际存储在数据库中的数据。

form = UserProfileForm(initial={'first_name': user.userprofile.first_name,
                                    'last_name': user.userprofile.last_name,
                                    'nickname': user.userprofile.nickname,
                                    'bio': user.userprofile.bio,
                                    'location': user.userprofile.location,
                                    'gender': user.userprofile.gender,
                                    'birth_date': user.userprofile.birth_date})

我已经创建了上面的代码并且它工作正常,但我认为它根本不是 pythonic,所以我的请求是关于我如何才能把它写得更好?

与其使用初始数据,不如将用户配置文件本身作为 instance 参数传递:

form = UserProfileForm(instance=user.userprofile)

假设 UserProfileForm 是一个 ModelForm,它应该是。