[Answer]-WTForms and Django OneToOneFields

0👍

✅

Seems it was easier than I thought.

Replace this:

form = AccountSettingsForm(request.POST, obj=request.user)

with:

form = AccountSettingsForm(request.POST, obj=request.user, country=request.user.account.country)

1👍

You can override the __init__() method like this to populate the data from obj.account (is that even the default name? I always use the related_name option).

class AccountSettingsForm(Form):
    def __init__(self, formdata=None, obj=None, prefix='', data={}, meta=None, **kwargs):
        data['country'] = obj.account.country
        # etc
        super(AccountSettingsForm, self).__init__(formdata, obj, prefix, 
                                                  data, meta, **kwargs)

Leave a comment