I have a django model which stores user and product data from a form input:
def ProductSelection(request, template_name=\'product_selection.html\'):
...
Just to clarify, the below code is how implemented the solution in my case:
project = Project.objects.create(
session=request.session.session_key,
# save all other fields
...
)
if request.user.is_authenticated():
project.user = request.user
else:
# make a copy of the session key
# this is done because the session_key changes
# on login/ register
request.session['key_copy'] = request.session.session_key
project.save()
And in my models.py:
class Project(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
...
So a user field can be null, and in this case we use the session_key to keep a track of things.