Using django ORM from web browser and from command line apps

Ian Kelly ian.g.kelly at gmail.com
Tue Jun 21 20:09:59 EDT 2011


On Tue, Jun 21, 2011 at 5:39 PM, News123 <news1234 at free.fr> wrote:
> Hi,
>
> I'm having a django browser application.
>
> There's certain administrative tasks, that I'd like to perform from the
> command line (cronjob or manually).
> As these scripts might be huge and might consume quite some memory I'd
> prefer, that they were not part of the normal application and would just
> consume memory during the administrative phase.
>
> I am not sure, whether this is a use case really being intended.

It sounds like you probably want a custom management command:

https://docs.djangoproject.com/en/1.3/howto/custom-management-commands/

> I wanted to know whether there are any precautions to take if I do this.
>
> The one issue, that I could imagine is that Django (if I understood
> correctly) performs normally database transaction based an a http request.

If you have the TransactionMiddleware enabled, yes.  Otherwise the
default is to commit everything immediately.

> What would happen if I don't do anythong special in a script.
> Would the entire runtime of the script be considered a transaction?

The default here is also to commit everything immediately.  Since
there is no HTTP request, the TransactionMiddleware does not get
invoked even if enabled.  For controlled transactions you will need to
use the commit_on_success or commit_manually decorator /
context-managers:

https://docs.djangoproject.com/en/1.3/topics/db/transactions/

Cheers,
Ian



More information about the Python-list mailing list