Django message framework – message particular user
Question:
In Django 1.3 and prior you could send a message to a particular user using:
user.message_set.create(message='some message text')
as of Django 1.4 this functionality has been removed in favor of the messaging framework. The messaging framework uses the following syntax:
info(request, 'some message text')
I am wondering if it is possible to queue a message for a particular user, as the old method allowed, rather than just for the user in request.user? So in a view I would want to be able to do something like the following:
susy = User.objects.get(username='susy')
# send a message to susy even though request.user is john
info(susy, 'some message text')
Answers:
Default storage engine for messages are now user cookies, meaning you cannot store message for anyone but current user. To work around this, you can try using session backend, fetching required user’s session and write a message directly there — just check how backend itself implements it in its source and use the same approach.
Obviously, this won’t work if your sessions are cookie-based as well.
If solution needs to be reliable and not depend on app configuration, you’ll need to create custom db-based solution for message handling. For example, you can implement your own db-based storage engine for messages
framework.
In Django 1.3 and prior you could send a message to a particular user using:
user.message_set.create(message='some message text')
as of Django 1.4 this functionality has been removed in favor of the messaging framework. The messaging framework uses the following syntax:
info(request, 'some message text')
I am wondering if it is possible to queue a message for a particular user, as the old method allowed, rather than just for the user in request.user? So in a view I would want to be able to do something like the following:
susy = User.objects.get(username='susy')
# send a message to susy even though request.user is john
info(susy, 'some message text')
Default storage engine for messages are now user cookies, meaning you cannot store message for anyone but current user. To work around this, you can try using session backend, fetching required user’s session and write a message directly there — just check how backend itself implements it in its source and use the same approach.
Obviously, this won’t work if your sessions are cookie-based as well.
If solution needs to be reliable and not depend on app configuration, you’ll need to create custom db-based solution for message handling. For example, you can implement your own db-based storage engine for messages
framework.