Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Using django ORM from web browser and from command line apps

Reply
Thread Tools

Using django ORM from web browser and from command line apps

 
 
News123
Guest
Posts: n/a
 
      06-21-2011
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.

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.

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

Are there any special commands that I have to use in order to indicate
when a transaction starts / stops?


Thanks in advance for suggestions.

Please look also at a related question.
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      06-22-2011
On Tue, Jun 21, 2011 at 5:39 PM, News123 <> 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....ment-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..../transactions/

Cheers,
Ian
 
Reply With Quote
 
 
 
 
News123
Guest
Posts: n/a
 
      06-22-2011
Hi Ian,

On 06/22/2011 02:09 AM, Ian Kelly wrote:
> On Tue, Jun 21, 2011 at 5:39 PM, News123 <> wrote:
>> 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).


> It sounds like you probably want a custom management command:
>
> https://docs.djangoproject.com/en/1....ment-commands/

Didn't know this existed.
Yes this is definitely a way to go. At least for part of the commands,
that I plan to implement.
Out of curiousity: Do you know whether the imports would be executed for
each potential command as soon as I call manage.py or only
'on demand'?

>
>> 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.


Good to know.
>
>> 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..../transactions/
>

Thanks this url is very clear.

I will use the commit model as required for my different tasks
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      06-22-2011
On Tue, Jun 21, 2011 at 6:21 PM, News123 <> wrote:
> Out of curiousity: Do you know whether the imports would be executed for
> each potential command as soon as I call manage.py or only
> 'on demand'?


Off the top of my head, I don't know.
 
Reply With Quote
 
News123
Guest
Posts: n/a
 
      06-22-2011
On 06/22/2011 03:08 AM, Ian Kelly wrote:
> On Tue, Jun 21, 2011 at 6:21 PM, News123 <> wrote:
>> Out of curiousity: Do you know whether the imports would be executed for
>> each potential command as soon as I call manage.py or only
>> 'on demand'?

>
> Off the top of my head, I don't know.


Never mind.
WIill jsut add a print statement when I make my first test.
This should clearly show whether the module is imported all the time or
only on demand.

 
Reply With Quote
 
bruno.desthuilliers@gmail.com
Guest
Posts: n/a
 
      06-22-2011
On Jun 22, 2:21*am, News123 <news1...@free.fr> wrote:
> Out of curiousity: Do you know whether the imports would be executed for
> each potential command as soon as I call manage.py or only
> 'on demand'?


Why would you care ? Just importing the module shouldn't have any side
effect.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using only the django ORM (DB access model) and nothing else. News123 Python 6 06-22-2011 01:16 AM
Question on Django and Django Book John Posner Python 0 11-13-2010 06:55 PM
.NET 2.0 Web Apps / .NET 3.5 ASP.NET Apps - Differences/Similariti london1919 MCTS 1 12-08-2008 03:16 PM
Which do you prefer? MCTS: Web Apps or MCTS: Windows Apps? Willy David Jr MCTS 3 01-09-2007 05:49 AM
Call windows apps from web apps anonymous ASP .Net Datagrid Control 4 02-28-2005 01:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57