On Jun 3, 12:18 pm, mikegolf <miga...@gmail.com> wrote:
> Hi,
> I've started to learn Django recently, however for last 5+ years I've
> been developing using PHP (especially Yii) and thus asking if there's
> any tutorial / documentation on significant differences in
> *thinking*.
> What I mean is for example objects' lifecycle - for PHP the life cycle
> of object is strictly related to the single request..
> I know that for Python / Django developers these are obvious things,
> but not for me. Thus I'd love to see an article / document which
> points these base and significant differences.
> Any recommendations?

Well, the answer may not be as simple as it seems, as it first
requires a correct understanding of Python's execution model,
namespaces, scopes and bindings (aka "variables") - and this would
make for a rather long and technical document. Then you have to know
how your django application is deployed.

I think the most important points wrt/ "objects lifcycle" are (overly
simplified):

* a Python module's top-level code is executed once the first time the
module is imported
* "import", "class" and "function" statements are executable
statements
* the code at the toplevel of a "class" statement is executed once
before the metaclass is called and the class object created
* all this will occur for each of your django server processes
* you can have multiple processes serving the same django application,
and ay request can be mapped to any process (this depends on the front
server and gateway so you have no control over this)

To make a long story short: remember you are in a long running
process, so never modify (mutate or rebind) any module or class
attribute when serving a request.

As an exemple, I once spent quite a few hours debugging a seemingly
intermittent and very very strang problem on a form. The root problem
was a younger co-worker wrote code that was mutating some of the
form's *class* attributes in the class initializer, and depending on
which process would process the form's submission, things would - or
not - get totally mixed up.

A more common mistake is to initialize a class or module date
attribute with the result of a call to datetime.datetime.now() and
wonder why it's not updated on each and every request.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to