5๐
โ
I managed to get it to work. It turns out that normally the session is only saved after the complete page is rendered.
I managed to save the session prematurely in my middleware using:
request.session.save()
I could then save my model like (note the _id after session, which allows you to set the foreign key using only an integer or varchar in my case):
visitor = Visitor()
visitor.session_id = request.session.session_key
visitor.save()
๐คSnels Nick
1๐
I was able to do this with the following, does it work for you?
Session.objects.get(session_key=request.session.session_key)
If not, perhaps @ilvar is correct that you attempting to access the session before it is active.
๐คFurbeenator
- [Django]-Defining django queryset with Q objects with foreign key
- [Django]-Django.core.exceptions.FieldError: Unknown field(s) (y, b, d, o)
- [Django]-Factory boy error : ValueError: save() prohibited to prevent data loss due to unsaved related object
- [Django]-How to upload a django file in memory to S3
Source:stackexchange.com