Opened 17 years ago

Closed 17 years ago

Last modified 17 years ago

#5534 closed (invalid)

stand-alone python script: no documentation avaliable

Reported by: anonymous Owned by: nobody
Component: Documentation Version: dev
Severity: Keywords: python script stand-alone
Cc: DXpublica@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

There is no documentation for how to write a bash python script for manage objects.
Typical use if we want to write python script for doing the same as we do in python manage shell (for example for bulk database updates)
For example, we want to run:

from mysite.polls.models import Poll
import datetime

i = 0
while i<4:

a = str(i)
print a
Poll.create(question=a, data=datetime.date.today())
i = i+1

in shell it's tedious to do. So maybe we could do it in bash:

#!/usr/bin/python
from mysite.polls.models import Poll
import datetime

i = 0
while i<4:

a = str(i)
print a
Poll.create(question=a, data=datetime.date.today())
i = i+1

But there is missing imports. In doc there is no mention

Thanks in advance,
Xan.

Change History (6)

comment:1 by anonymous, 17 years ago

Cc: DXpublica@… added

comment:2 by pytechd, 17 years ago

You can do this yourself easily:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
from django.conf import settings
from myproject.polls.models import Poll

This has come up before so it should probably be added somewhere to the documentation. If it's already there, I can't find it.

comment:4 by DXpublica@…, 17 years ago

Many thanks for your answers.

Xan.

in reply to:  2 ; comment:5 by anonymous, 17 years ago

Replying to pytechd:

You can do this yourself easily:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
from django.conf import settings
from myproject.polls.models import Poll

This has come up before so it should probably be added somewhere to the documentation. If it's already there, I can't find it.

I tried some code, and the method that _works_ for me is:

First, set DJANGO_SETTINGS_MODULE:

export DJANGO_SETTINGS_MODULE=analitzador.settings

Secondly, write the script:

import os, sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.conf import settings
from mysite.polls.models import Poll

#Anything you want:
print Poll.objects.all()

If the script is in the mysite directory, all work fun! (for example, if mysite is in /home/user/mysite) but if script is in somewhere else, it does not run (for example mysite is in /home/user/mysite and the script is in /home/user/mysite/scripts)

How can I solve that?

Thanks,
Xan.

in reply to:  5 comment:6 by DXpublica@…, 17 years ago

Replying to anonymous:

Replying to pytechd:

You can do this yourself easily:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
from django.conf import settings
from myproject.polls.models import Poll

This has come up before so it should probably be added somewhere to the documentation. If it's already there, I can't find it.

I tried some code, and the method that _works_ for me is:

First, set DJANGO_SETTINGS_MODULE:

export DJANGO_SETTINGS_MODULE=analitzador.settings

Secondly, write the script:

import os, sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.conf import settings
from mysite.polls.models import Poll

#Anything you want:
print Poll.objects.all()

If the script is in the mysite directory, all work fun! (for example, if mysite is in /home/user/mysite) but if script is in somewhere else, it does not run (for example mysite is in /home/user/mysite and the script is in /home/user/mysite/scripts)

How can I solve that?

Thanks,
Xan.

Definitively, the script is:

import os, sys
# Add this line if your script is not in your app directory
sys.path.append('[super directory of app path]') # If your project is in '/home/user/mysite/polls', you have to put sys.path.append('/home/user/mysite/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.conf import settings
from mysite.polls.models import Poll
 
#Anything you want:
print Poll.objects.all()

I only put this rectification for documentation purposes.

Thanks,
Xan.

Note: See TracTickets for help on using tickets.
Back to Top