Isaac
Isaac

Reputation: 840

Django: passing an object as argument to a view

Is there a 'convenient' way to pass generic, dynamically created objects from one view as arguments to another view? As far as I can tell from the documentation, only strings can be passed.

I was thinking of two work arounds:

  1. Base the object on a model, save the model and pass only the model id (which is a string) to the next view. This view can then load the object again.
  2. Serializing the object, and pass the serialized object (which now is a string) to the next view. This view can then deserialize the object.

Maybe I miss something? Or if not, does anyone has an opinion on the workarounds?

Upvotes: 0

Views: 1833

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599470

Rather than passing it to the view as a string, you could save it in the session in the first view, and pick it up from there in the second. The session should be able to accept your raw object, but potentially you might need to explicitly serialize it first.

Upvotes: 1

Randy
Randy

Reputation: 938

As far as I can tell, you can pass a django model instance to another view. Python will allow you to take any object instance as an argument. Views certainly aren't limited to strings (you can definitely pass ints, tuples, lists, dictionaries, etc...) Are you getting a specific error when attempting to do this?

Upvotes: 0

Related Questions