@giles,
This is happening within the context of a single request.
@Cartoo
You are exactly right. I'm trying to pass the request object, which includes the session object, to a chunk of Django 1.5 code that is tied to the model (model.clean). When running the Admin app, it does not have access to the request object.
So I tried stashing it in RAM only to get a crash course in how "shared" RAM is!
I have several choices I'm trying to figure out the one with the least amount of work. I would not be working in Automation if I weren't lazy.....
My goal is to auto-fill the company division the current session belongs to. This allows us to use the same business software for multiple divisions. Django 1.4 and 1.5 Admin has a lovely built-in easy method to filter records before they are displayed (ModelAdmin.queryset), the example given in the docs shows how to limit records to the user that "owns" them. I just changed that to "company division" (the list is stored in Site database), and presto! All data displayed anyplace inside the admin is restricted and the operation is totally transparent to the user.
What I need to do now is have an automatic method to populate each record's "site" field with the "site" value inside the current session.
- I can copy the admin view and make changes to it. Since it is passed the request, I can access the session object to get the seed value. This would be the "best practice" when I (eventually) get approval to replace the admin with our own task-based system. But I suspect its a lot of work to try it inside of the admin framework, It may have a domino effect resulting in the editing of a lot more related files. The html templates, for example.
- Which brings me to copying just the templates and changing them, since I think they are sent the session data as part of the request object. I have not learned how to work with templates, so I cannot judge how much effort this represents.
- Find some other mechanism within the admin framework. Find a place where I have access to both the data instance and the session data. Or a way I can pass one or the other to some code where I can make use of it.
Its the 3rd possibility I'm trolling for here. Since I don't have time to do choice 1 - at least the full custom version. And I dunno about #2.
There is another Django 1.4/1.5 feature I might be able to use: ModelAdmin.get_form. This is called to "get" the add, change or delete form. And it is passed the request object. I just have to learn if it also has access to the data instance. Or maybe I can pass "default" values as a keyword argument? I have a vague memory reading something about that. Have to dig back into the Admin docs and/or read the source code....
As always, any input, thoughts, or suggestions welcome!