I want to store some things in a database, and am using the current session as a foreign key: from models.py
class Visited(models.Model):
session = models.ForeignKey(Session)
page = models.ForeignKey(Page)
times_visited = models.PositiveIntegerField()
ip_address = models.IPAddressField()
date_last_visited = models.DateTimeField()
def __unicode__(self):
return u'(%s, %s)' % (str(self.session.session_key), str(self.page.name))
To make a new entry for this model I'm using the following to get the current session (in views.py):
Session.objects.get(session_key=request.session.session_key)
However if it is the first time a user has visited the site, and thus do not have a cookie set yet, the code above will produce a DoesNotExist
error.
I know that even if there is now cookie set you can still set session objects. So I can think of a few hacks to make this work, such as:
- Set unique identifier as a session object (in addition to the session key)
- Temporarily store the data I wish to add to the database a session object, and use a decorator function to check if it exists before using a session.
- Just use the session objects and not store anything in the database (this would be technically possible, but for my implementation it would depend on Python dictionaries -with a few hundred entries- being at least as efficient as a database for things like sorting.)
But I would like a better solution I can live with. Are there any generally used or good solutions to this problem? Or am I even referencing sessions properly in my model?
Thank you for your help.