0๐
@Martijn is or might be the correct way to save the object in session variables.
But the issue was solved by moving back to Django 1.5. So this issue is specifically for django 1.6.2.
Hope this helps.
24๐
In a session, Iโd just store the object primary key:
request.session['card'] = card.id
and when loading the card from the session, obtain the card again with:
try:
card = Card.objects.get(id=request.session['card'])
except (KeyError, Card.DoesNotExist):
card = None
which will set card
to None
if there isnโt a card
entry in the session or the specific card doesnโt exist.
By default, session data is serialised to JSON. You could also provide your own serializer, which knows how to store the card.id
value or some other representation and, on deserialization, produce your Card
instance again.
- Django sub-applications & module structure
- Sphinx and re-usable Django apps
- How do I use perform_create to set a field automatically in Django Rest Framework?
- How to mock chained function calls in python?
- Reverse Queryset Order in Django
4๐
Unfortunately the suggested answer does not work if the object is not a database object but some other kind of object โ say, datetime or an object class Foo(object): pass that isnโt a database model object.
Sure, if the object happen to have some id field you can store the id field in the database and look up the value from there but in general it may not have such a simple value and the only way is to convert the data to string in such a way that you can read that string and reconstruct the object based on the information in the string.
In the case of a datetime object this is made more complicated by the fact that while a naive datetime object can print out format %Z by simply not printing anything, the strptime object cannot read format %Z if there is nothing, it will choke unless there is a valid timezone specification there โ so if you have a datetime object that may or may not contain a tzinfo field you really have to do strptime twice once with %Z and then if it chokes without the %Z. This is silly. It is made even sillier by the fact that datetime objects have a fromtimestamp function but no totimestamp function that uniformly produces a timestamp that fromtimestamp will read. If there is a format code that produces timestamp number I havenโt found one and again, strftime/strptime suffer from the fact that they are not symmetric as described above.
3๐
There are two simple ways to do this.
- If each object belongs to a single session at the same time, store session id as a model field, and update models.
- If an object can belong to multiple sessions at the same time, store object.id as a session variable.
0๐
โ> DONโT EVER FEEL LIKE DOING SOMETHING LIKE THIS! <โ
Django using session and ctypes: Plz read Martijn Pieters explanation comment below.
class MyObject:
def stuff(self):
return "stuff..."
my_obj = MyObject()
request.session['id_my_obj'] = id(my_obj)
...
id_my_obj = request.session.get('id_my_obj')
import ctypes
obj = ctypes.cast(id_my_obj, ctypes.py_object).value
print(obj.stuff())
# returns "stuff..."
- What is the difference between the create and perform_create methods in Django rest-auth
- Git aws.push: No module named boto
- Advanced Django Template Logic
- How to use select_related with GenericForeignKey in django?
- Trying to pass a QuerySet as initial data to a formset
0๐
Objects cannot be stored in session from Django 1.6 or above.
If you donโt want to change the behavior of the cookie value(of a object), you can add a dictionary there. This can be used for non database objects like class object etc.
from django.core.serializers.json import DjangoJSONEncoder
import json
card_dict = card.__dict__
card_dict .pop('_state', None) #Pop which are not json serialize
card_dict = json.dumps(card_dict , cls=DjangoJSONEncoder)
request.session['card'] = card_dict
Hope it will help you!
- How show personalized error with get_object_or_404
- Force delete of any previous test database (autoclobber) when running Django unit tests, eg, in PyCharm
- Django: How to keep the test database when the test is finished?
0๐
In my case, as the object is not serializable (selenium webdriver), I had to use global variables, which is working great.
- All the values of the many to many field : Django
- Django form dropdown list of stored models
- Django โ authentication, registration with email confirmation