1

i have a form say:

class ProfileEditForm(forms.Form):
    first_name = forms.CharField(max_length=20)
    last_name = forms.CharField(max_length=20)
    email = forms.EmailField(max_length=50)
    address = forms.CharField(max_length=100)

I want to pass model instance on it so that when user tries to edit their profile they get their existing data on the form.

For some reasons I am not using ModelForm

lets say I have instance user = User.objects.get(pk=pk)

and I want to pass instance like form = ProfileEditForm(instance=user)

I googled and found I can only use instance with model form but can I use it in form too that not from model ?

Thank you

5
  • Can you give the reasons you are not using ModelForm? You should. Commented Aug 3, 2015 at 7:49
  • @DanielRoseman because my supervisor said so. He just want to use form only. Is there anyway I can get this ? Commented Aug 3, 2015 at 7:59
  • I don't know what kind of "supervisor" tells you to use the wrong class. To update a model instance, use a ModelForm. Commented Aug 3, 2015 at 8:05
  • Thank you. Except model form i cant do it right ? Commented Aug 3, 2015 at 8:08
  • You could write a custom constructor for the Form to simulate the modelform, but like Daniel said: just use the right tool for the task, a ModelForm. Commented Aug 3, 2015 at 9:26

1 Answer 1

1

Try this:

user = User.objects.filter(pk=pk).values('first_name','last_name','email','address')
form = ProfileEditForm(user[0])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.