Pass objects to another template without saving first (django)
Is there a way to pass a newly created object(not saved yet) to another page?
Currently, I have to save object A in page A before moving on to page B. After saving object A, I am able to get the id of object A and get all information about the new Object A from database, and this will aid in the creation of object B in template B.
The problem is because I was thinking what if the user decides to cancel operations on page B? By the time he decides not to create object B in page B, object A is already created according to above scenario, which I don't want this to happen. The whole process must create both objects and not just one model hanging. Even I do a delete of the previous object A, this will disrupt the increment of id of table A. Maybe doing an reset auto increment will do the trick, but i don't think its recommended.
I don't think i am able to pass Queryset objects(not 开发者_如何学Pythonsaved) to template page B because it confuses me with the numerous if request.method == POST
in one definition view and to counter this, to navigate to template page B , I used a different URL(diff def view) instead of giving a different template in the same definition.
Why I need to navigate to another page/view to create object B instead of displaying all fields in object A and object B in one page, because object B fields will vary and depends on what object A is. (Not planning to use dynamic pages)
I hope I'm not confusing anyone. I'll provide more details if needed. Thanks.
One possible way is using sessions
Lets say your model definition is as follows...
class MyClass(Model):
...
...
Probably you are using
rec = MyClass(params...)
instead of
rec = MyClass.objects.create(...)
so, after you create an instance (with or without saving to database) you can store that instance in session
request.session['somedata'] = rec
And when you wish to get it
request.session.get('somedata')
Looks like the django form wizard is what you are searching for.
精彩评论