12
If you are using the admin site why not use a custom model admin
class MyModelAdmin( admin.ModelAdmin ):
def save_model( self, request, obj, form, change ):
#pre save stuff here
obj.save()
#post save stuff here
admin.site.register( MyModel, MyModelAdmin )
A signal is something that is fired every time the object is saved regardless of if it is being done by the admin or some process that isn’t tied to a request and isn’t really an appropriate place to be doing request based actions
38
Being reluctant to mess around with thread-local state, I decided to try a different approach. As far as I can tell, the post_save
and pre_save
signal handlers are called synchronously in the thread that calls save()
. If we are in the normal request handling loop, then we can just walk up the stack to find the request object as a local variable somewhere. e.g.
from django.db.models.signals import pre_save
from django.dispatch import receiver
@receiver(pre_save)
def my_callback(sender, **kwargs):
import inspect
for frame_record in inspect.stack():
if frame_record[3]=='get_response':
request = frame_record[0].f_locals['request']
break
else:
request = None
...
If there’s a current request, you can grab the user
attribute from it.
Note: like it says in the inspect
module docs,
This function relies on Python stack frame support in the interpreter, which isn’t
guaranteed to exist in all implementations of Python.
- [Django]-Excluding directory
- [Django]-Alowing 'fuzzy' translations in django pages?
- [Django]-"Failed building wheel for psycopg2" – MacOSX using virtualenv and pip
3
You can use a middleware to store the current user: http://djangosnippets.org/snippets/2179/
Then you would be able to get the user with get_current_user()
- [Django]-How do I start up remote debugging with PyCharm?
- [Django]-Is Tornado a replacement to Django or are they complementary to each other?
- [Django]-Is Django admin difficult to customize?
2
We can solve this problem using middleware classes.
Create singleton class in where will be storing user variable.
class Singleton(type):
'''
Singleton pattern requires for GetUser class
'''
def __init__(cls, name, bases, dicts):
cls.instance = None
def __call__(cls, *args, **kwargs):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls.instance
class NotLoggedInUserException(Exception):
'''
'''
def __init__(self, val='No users have been logged in'):
self.val = val
super(NotLoggedInUser, self).__init__()
def __str__(self):
return self.val
class LoggedInUser(object):
__metaclass__ = Singleton
user = None
def set_user(self, request):
if request.user.is_authenticated():
self.user = request.user
@property
def current_user(self):
'''
Return current user or raise Exception
'''
if self.user is None:
raise NotLoggedInUserException()
return self.user
@property
def have_user(self):
return not user is None
Create own middleware class that will be setting user for LoggedInUser instance,and insert out middleware after ‘django.contrib.auth.middleware.AuthenticationMiddleware’ in settings.py
from useranytimeaccess import LoggedInUser
class LoggedInUserMiddleware(object):
'''
Insert this middleware after django.contrib.auth.middleware.AuthenticationMiddleware
'''
def process_request(self, request):
'''
Returned None for continue request
'''
logged_in_user = LoggedInUser()
logged_in_user.set_user(request)
return None
In signals import LoggedInUser class and get current user
logged_in = LoggedInUser()
user = logged_in.user
- [Django]-Boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
- [Django]-How can I tell the Django ORM to reverse the order of query results?
- [Django]-Access tuple in django template
0
Will I do it like this I am still looking for a better solution if I am not using the admin site
@receiver(post_save, sender=FlowList)
def flow_list_post_save(sender, instance, created, **kwargs):
import inspect
request = None
try:
request = next((frame[0].f_locals['request'] for frame in inspect.stack() if frame[3] == 'get_response'), None)
except StopIteration:
pass
- [Django]-How to fix error "ERROR: Command errored out with exit status 1: python." when trying to install django-heroku using pip
- [Django]-Get SQL query count during a Django shell session
- [Django]-Make clicked tab active in Bootstrap