0

I have a form which has is an instance of some model X.Now how can I access the form's instance in a view provided that I'm handling form submission(POST) in another view.One view is used to create the form and other view is used to process the form.

2 Answers 2

1

From the documentation

Also, a model form instance bound to a model object will contain a self.instance attribute that gives model form methods access to that specific model instance.

def myview(request):
    if request.method == "POST":
        form = MyModelForm(request.POST,request.FILES)
        # form.instance -- this is the model
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply pass the form object off to the secondary view:

def view_one(request, slug):
    if request.method == 'POST':
        obj = get_object_or_404(Model, slug=slug)
        model_form = MyModelForm(request.POST, instance = obj)
        return view_two(request, form=model_form) 

def view_two(request, form=None):
    if form:
        obj = form.save(commit=False)
        obj.some_attribute = "Foo"
        obj.save()
        return render_to_response(...)

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.