Using django ORM from web browser and from command line apps

Discussion in 'Python' started by News123, Jun 22, 2011.

  1. News123

    News123 Guest

    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.
     
    News123, Jun 22, 2011
    #1
    1. Advertising

  2. News123

    Ian Kelly Guest

    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.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
     
    Ian Kelly, Jun 22, 2011
    #2
    1. Advertising

  3. News123

    News123 Guest

    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.3/howto/custom-management-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.3/topics/db/transactions/
    >

    Thanks this url is very clear. :)

    I will use the commit model as required for my different tasks
     
    News123, Jun 22, 2011
    #3
  4. News123

    Ian Kelly Guest

    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.
     
    Ian Kelly, Jun 22, 2011
    #4
  5. News123

    News123 Guest

    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.
     
    News123, Jun 22, 2011
    #5
  6. News123

    Guest

    On Jun 22, 2:21 am, 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'?


    Why would you care ? Just importing the module shouldn't have any side
    effect.
     
    , Jun 22, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Posner

    Question on Django and Django Book

    John Posner, Nov 13, 2010, in forum: Python
    Replies:
    0
    Views:
    345
    John Posner
    Nov 13, 2010
  2. News123
    Replies:
    6
    Views:
    1,778
    News123
    Jun 22, 2011
  3. anonymous

    Call windows apps from web apps

    anonymous, Feb 22, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    250
    anonymous
    Feb 28, 2005
  4. eGenix Team: M.-A. Lemburg
    Replies:
    0
    Views:
    156
    eGenix Team: M.-A. Lemburg
    Jun 18, 2013
  5. Gary Roach
    Replies:
    0
    Views:
    156
    Gary Roach
    Jan 2, 2014
Loading...

Share This Page