rsinger86/django-lifecycle

How can I pass the request object into hook code

Closed this issue · 2 comments

Hello.

In some parts of my django application I use the "self.request" object, e.g. in class based views, when I want to send an email to a user. With django-lifecycle I can't to do it.

So, can someone to give me an advice in this problem? Thanks.

Hmmm, typically I think you wouldn't want to be taking inputs from the request so deep into your application. I guess you could try setting a request context variable on your model instance from the view, and then you could access it from the hooked method.

That doesn't quite feel right to me, tho. You may consider a separate service module (more info here) for business logic that doesn't neatly fit into a model method. Your view could pass the model instance to the service function, which could inspect the model via this library's has_changed and initial_value methods to determine if an email should be sent.

I think I can solve this problem with services. Thanks for your reply!